Skip to content

Commit 92ecf5d

Browse files
authored
Merge pull request #431 from phpDocumentor/phpstan-improvements
More phpstan checks for better code
2 parents f083594 + 9f96a9f commit 92ecf5d

40 files changed

+693
-199
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"phpstan/phpstan-mockery": "^1.1",
3030
"phpstan/extension-installer": "^1.1",
3131
"phpstan/phpstan-webmozart-assert": "^1.2",
32-
"psalm/phar": "^5.26"
32+
"psalm/phar": "^5.26",
33+
"shipmonk/dead-code-detector": "^0.5.1"
3334
},
3435
"autoload": {
3536
"psr-4": {

composer.lock

Lines changed: 70 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline.neon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Unused phpDocumentor\\\\Reflection\\\\DocBlock\\\\ExampleFinder\\:\\:getExampleDirectories$#"
5+
count: 1
6+
path: src/DocBlock/ExampleFinder.php
7+
8+
-
9+
message: "#^Unused phpDocumentor\\\\Reflection\\\\DocBlock\\\\ExampleFinder\\:\\:setExampleDirectories$#"
10+
count: 1
11+
path: src/DocBlock/ExampleFinder.php
12+
13+
-
14+
message: "#^Unused phpDocumentor\\\\Reflection\\\\DocBlock\\\\ExampleFinder\\:\\:setSourceDirectory$#"
15+
count: 1
16+
path: src/DocBlock/ExampleFinder.php
17+
18+
-
19+
message: "#^Unused phpDocumentor\\\\Reflection\\\\DocBlock\\\\Tags\\\\Factory\\\\MethodParameterFactory\\:\\:formatNull$#"
20+
count: 1
21+
path: src/DocBlock/Tags/Factory/MethodParameterFactory.php

phpstan.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
14
parameters:
25
level: max
36
ignoreErrors:
47
- '#Method phpDocumentor\\Reflection\\DocBlock\\StandardTagFactory::createTag\(\) should return phpDocumentor\\Reflection\\DocBlock\\Tag but returns mixed#'
58
- '#Offset 2 on array\{string, 28, int\} on left side of \?\? always exists and is not nullable\.#'
9+
-
10+
path: src/DocBlockFactoryInterface.php
11+
identifier: shipmonk.deadMethod
12+
-
13+
path: src/DocBlock/TagFactory.php
14+
identifier: shipmonk.deadMethod
615
paths:
716
- src
17+
- tests/unit

phpunit.xml.dist

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
3-
<phpunit
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
6-
colors="true"
7-
convertDeprecationsToExceptions="false"
8-
beStrictAboutOutputDuringTests="false"
9-
forceCoversAnnotation="true"
10-
verbose="true"
11-
bootstrap="vendor/autoload.php"
12-
>
13-
<testsuites>
14-
<testsuite name="unit">
15-
<directory>./tests/unit</directory>
16-
</testsuite>
17-
<testsuite name="integration">
18-
<directory>./tests/integration</directory>
19-
</testsuite>
20-
</testsuites>
21-
<filter>
22-
<whitelist>
23-
<directory suffix=".php">./src/</directory>
24-
</whitelist>
25-
</filter>
26-
<logging>
27-
<log type="coverage-html"
28-
target="build/coverage"
29-
lowUpperBound="35"
30-
highLowerBound="70"/>
31-
<log type="coverage-clover" target="build/logs/clover.xml"/>
32-
</logging>
33-
<listeners>
34-
<listener
35-
class="Mockery\Adapter\Phpunit\TestListener"
36-
file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"
37-
/>
38-
</listeners>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" colors="true" convertDeprecationsToExceptions="false" beStrictAboutOutputDuringTests="false" forceCoversAnnotation="true" verbose="true" bootstrap="vendor/autoload.php">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src/</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="build/logs/clover.xml"/>
9+
<html outputDirectory="build/coverage" lowUpperBound="35" highLowerBound="70"/>
10+
</report>
11+
</coverage>
12+
<testsuites>
13+
<testsuite name="unit">
14+
<directory>./tests/unit</directory>
15+
</testsuite>
16+
<testsuite name="integration">
17+
<directory>./tests/integration</directory>
18+
</testsuite>
19+
</testsuites>
20+
<logging/>
21+
<listeners>
22+
<listener class="Mockery\Adapter\Phpunit\TestListener" file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"/>
23+
</listeners>
3924
</phpunit>

src/DocBlock/StandardTagFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
2323
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ImplementsFactory;
2424
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodFactory;
25+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MixinFactory;
2526
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory;
2627
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory;
2728
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
2829
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
2930
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
30-
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateExtendsFactory;
31+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory;
3132
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory;
32-
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateImplementsFactory;
3333
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory;
3434
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
3535
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
@@ -151,11 +151,11 @@ public static function createInstance(FqsenResolver $fqsenResolver): self
151151
new PropertyReadFactory($typeResolver, $descriptionFactory),
152152
new PropertyWriteFactory($typeResolver, $descriptionFactory),
153153
new MethodFactory($typeResolver, $descriptionFactory),
154+
new MixinFactory($typeResolver, $descriptionFactory),
154155
new ImplementsFactory($typeResolver, $descriptionFactory),
155156
new ExtendsFactory($typeResolver, $descriptionFactory),
156157
new TemplateFactory($typeResolver, $descriptionFactory),
157-
new TemplateImplementsFactory($typeResolver, $descriptionFactory),
158-
new TemplateExtendsFactory($typeResolver, $descriptionFactory),
158+
new TemplateCovariantFactory($typeResolver, $descriptionFactory),
159159
new ThrowsFactory($typeResolver, $descriptionFactory),
160160
);
161161

src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use phpDocumentor\Reflection\Types\Context as TypeContext;
1919
use PHPStan\PhpDocParser\Lexer\Lexer;
2020
use PHPStan\PhpDocParser\Parser\ConstExprParser;
21+
use PHPStan\PhpDocParser\Parser\ParserException;
2122
use PHPStan\PhpDocParser\Parser\PhpDocParser;
2223
use PHPStan\PhpDocParser\Parser\TokenIterator;
2324
use PHPStan\PhpDocParser\Parser\TypeParser;
@@ -59,13 +60,17 @@ public function __construct(PHPStanFactory ...$factories)
5960

6061
public function create(string $tagLine, ?TypeContext $context = null): Tag
6162
{
62-
$tokens = $this->tokenizeLine($tagLine . "\n");
63-
$ast = $this->parser->parseTag($tokens);
64-
if (property_exists($ast->value, 'description') === true) {
65-
$ast->value->setAttribute(
66-
'description',
67-
rtrim($ast->value->description . $tokens->joinUntil(Lexer::TOKEN_END), "\n")
68-
);
63+
try {
64+
$tokens = $this->tokenizeLine($tagLine . "\n");
65+
$ast = $this->parser->parseTag($tokens);
66+
if (property_exists($ast->value, 'description') === true) {
67+
$ast->value->setAttribute(
68+
'description',
69+
rtrim($ast->value->description . $tokens->joinUntil(Lexer::TOKEN_END), "\n")
70+
);
71+
}
72+
} catch (ParserException $e) {
73+
return InvalidTag::create($tagLine, '')->withError($e);
6974
}
7075

7176
if ($context === null) {
@@ -80,6 +85,8 @@ public function create(string $tagLine, ?TypeContext $context = null): Tag
8085
}
8186
} catch (RuntimeException $e) {
8287
return InvalidTag::create((string) $ast->value, 'method')->withError($e);
88+
} catch (ParserException $e) {
89+
return InvalidTag::create((string) $ast->value, $ast->name)->withError($e);
8390
}
8491

8592
return InvalidTag::create(

src/DocBlock/Tags/Factory/MethodParameterFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function format($defaultValue): string
3434
{
3535
$method = 'format' . ucfirst(gettype($defaultValue));
3636
if (method_exists($this, $method)) {
37-
return ' = ' . $this->{$method}($defaultValue);
37+
return $this->{$method}($defaultValue);
3838
}
3939

4040
return '';

src/DocBlock/Tags/Factory/TemplateExtendsFactory.php renamed to src/DocBlock/Tags/Factory/TemplateCovariantFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
88
use phpDocumentor\Reflection\DocBlock\Tag;
9-
use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends;
9+
use phpDocumentor\Reflection\DocBlock\Tags\TemplateCovariant;
1010
use phpDocumentor\Reflection\TypeResolver;
1111
use phpDocumentor\Reflection\Types\Context;
12-
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
1312
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
13+
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
14+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1415
use Webmozart\Assert\Assert;
1516

1617
use function is_string;
1718

1819
/**
1920
* @internal This class is not part of the BC promise of this library.
2021
*/
21-
final class TemplateExtendsFactory implements PHPStanFactory
22+
final class TemplateCovariantFactory implements PHPStanFactory
2223
{
2324
private DescriptionFactory $descriptionFactory;
2425
private TypeResolver $typeResolver;
@@ -31,21 +32,21 @@ public function __construct(TypeResolver $typeResolver, DescriptionFactory $desc
3132

3233
public function supports(PhpDocTagNode $node, Context $context): bool
3334
{
34-
return $node->value instanceof ExtendsTagValueNode && $node->name === '@template-extends';
35+
return $node->value instanceof TemplateTagValueNode && $node->name === '@template-covariant';
3536
}
3637

3738
public function create(PhpDocTagNode $node, Context $context): Tag
3839
{
3940
$tagValue = $node->value;
40-
Assert::isInstanceOf($tagValue, ExtendsTagValueNode::class);
41+
Assert::isInstanceOf($tagValue, TemplateTagValueNode::class);
4142

4243
$description = $tagValue->getAttribute('description');
4344
if (is_string($description) === false) {
4445
$description = $tagValue->description;
4546
}
4647

47-
return new TemplateExtends(
48-
$this->typeResolver->createType($tagValue->type, $context),
48+
return new TemplateCovariant(
49+
$this->typeResolver->createType(new IdentifierTypeNode($tagValue->name), $context),
4950
$this->descriptionFactory->create($description, $context)
5051
);
5152
}

src/DocBlock/Tags/Factory/TemplateImplementsFactory.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)