Skip to content

Commit 777f1fe

Browse files
Updated unit tests
1 parent fb9cfaf commit 777f1fe

13 files changed

+1293
-36
lines changed

.travis.coverage.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set -x
2+
if [ "$TRAVIS_PHP_VERSION" = '7.1' ] ; then
3+
wget https://scrutinizer-ci.com/ocular.phar
4+
php ocular.phar code-coverage:upload --format=php-clover ./clover.xml
5+
fi

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- '7.1'
5+
6+
before_script:
7+
- composer install
8+
9+
script:
10+
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=clover.xml --colors
11+
12+
after_script:
13+
- sh .travis.coverage.sh

src/Formatter/ClassFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public function format(bool $ignoreSubElements = false): string
8181

8282
// remove sub interfaces of other interfaces
8383
$interfaces = array_filter($interfaces, function (ReflectionClass $interface) use ($interfaces) {
84+
$interfaceName = $interface->getName();
8485
foreach ($interfaces as $compareInterface) {
8586
/**
8687
* @var ReflectionClass $compareInterface
8788
*/
88-
if ($interface->implementsInterface($compareInterface->getName())) {
89+
if ($compareInterface->implementsInterface($interfaceName)) {
8990
return false;
9091
}
9192
}

src/Formatter/ConstantFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function format(): string
3737
{
3838
$n = PhpStubGenerator::$eol;
3939
$t = PhpStubGenerator::$tab;
40+
if (!$this->class->hasConstant($this->constantName)) {
41+
return '';
42+
}
4043

4144
$parentClass = null;
4245
try {

src/Formatter/FunctionFormatter.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ protected function formatParams(): string
2727
$params = [];
2828
foreach ($this->function->getParameters() as $parameter) {
2929
$param = '';
30-
$type = (string) $parameter->getType();
30+
$type = $parameter->getType();
31+
if ($type instanceof ReflectionType) {
32+
$typeAllowsNull = $type->allowsNull();
33+
$type = (string) $type;
34+
} else {
35+
$type = '';
36+
}
3137

3238
if ($type !== '') {
33-
if ($parameter->allowsNull() &&
34-
(!$parameter->isDefaultValueAvailable() || $parameter->getDefaultValue() !== null)
35-
) {
39+
if ($typeAllowsNull) {
3640
$param .= '?';
3741
}
3842

@@ -73,6 +77,7 @@ protected function formatReturnType(): string
7377

7478
if ($this->function->hasReturnType()) {
7579
$returnType = $this->function->getReturnType();
80+
7681
if ($returnType instanceof ReflectionType) {
7782
$allowsNull = $returnType->allowsNull();
7883
$returnType = (string) $returnType;
@@ -95,11 +100,13 @@ public function format(): string
95100
$t = PhpStubGenerator::$tab;
96101

97102
$result = '';
98-
$result .= FormatHelper::indentDocBlock((string) $this->function->getDocComment(), 1, $t) . $n
99-
. $t;
103+
$doc = $this->function->getDocComment();
104+
if (is_string($doc)) {
105+
$result .= FormatHelper::indentDocBlock($doc, 1, $t) . $n;
106+
}
100107

101-
$result .= 'function ' . $this->function->getName() . '(' . $this->formatParams() . ')';
102-
$this->formatReturnType();
108+
$result .= $t . 'function ' . $this->function->getName() . '(' . $this->formatParams() . ')';
109+
$result .= $this->formatReturnType();
103110

104111
$result .= ' {}' . $n . $n;
105112

src/Formatter/MethodFormatter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818
class MethodFormatter extends FunctionFormatter
1919
{
20-
const DEFAULT_TYPES = ['int', 'float', 'bool', 'string', 'self', 'callable', 'array', 'object'];
21-
2220
/**
2321
* @var string
2422
*/
@@ -46,9 +44,12 @@ public function format(): string
4644
}
4745

4846
$result = '';
49-
$result .= FormatHelper::indentDocBlock((string) $this->function->getDocComment(), 2, $t) . $n
50-
. $t . $t;
47+
$doc = $this->function->getDocComment();
48+
if (is_string($doc)) {
49+
$result .= FormatHelper::indentDocBlock($doc, 2, $t) . $n;
50+
}
5151

52+
$result .= $t . $t;
5253
if (!$this->classIsInterface && $this->function->isAbstract()) {
5354
$result .= 'abstract ';
5455
} elseif ($this->function->isFinal()) {
@@ -59,7 +60,7 @@ public function format(): string
5960
$result .= 'public ';
6061
} elseif ($this->function->isProtected()) {
6162
$result .= 'protected ';
62-
} else {
63+
} elseif ($this->function->isPrivate()) {
6364
$result .= 'private ';
6465
}
6566

src/Formatter/PropertyFormatter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace setasign\PhpStubGenerator\Formatter;
55

6-
use ReflectionClass;
76
use ReflectionProperty;
87
use setasign\PhpStubGenerator\Helper\FormatHelper;
98
use setasign\PhpStubGenerator\PhpStubGenerator;
@@ -38,14 +37,17 @@ public function format(): string
3837
}
3938

4039
$result = '';
41-
$result .= FormatHelper::indentDocBlock((string) $this->property->getDocComment(), 2, $t) . $n
42-
. $t . $t;
40+
$doc = $this->property->getDocComment();
41+
if (is_string($doc)) {
42+
$result .= FormatHelper::indentDocBlock($doc, 2, $t) . $n;
43+
}
4344

45+
$result .= $t . $t;
4446
if ($this->property->isPublic()) {
4547
$result .= 'public ';
4648
} elseif ($this->property->isProtected()) {
4749
$result .= 'protected ';
48-
} else {
50+
} elseif ($this->property->isPrivate()) {
4951
$result .= 'private ';
5052
}
5153

src/PhpStubGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function generate(): string
7474
* @var ReflectionFunction[] $functions
7575
*/
7676
foreach ($functions as $function) {
77-
$isGlobalNamespace = $namespace === '';
77+
$isGlobalNamespace = ($namespace === '');
7878
$result .= 'namespace' . (!$isGlobalNamespace ? ' ' . $namespace : '') . $n
7979
. '{' . $n;
8080
$result .= $this->formatAliases(

0 commit comments

Comments
 (0)