Skip to content

Commit 3f6d648

Browse files
committed
More phpstan checks for better code
1 parent f083594 commit 3f6d648

26 files changed

+154
-92
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.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@ parameters:
33
ignoreErrors:
44
- '#Method phpDocumentor\\Reflection\\DocBlock\\StandardTagFactory::createTag\(\) should return phpDocumentor\\Reflection\\DocBlock\\Tag but returns mixed#'
55
- '#Offset 2 on array\{string, 28, int\} on left side of \?\? always exists and is not nullable\.#'
6+
-
7+
path: src/DocBlockFactoryInterface.php
8+
identifier: shipmonk.deadMethod
9+
-
10+
path: src/DocBlock/TagFactory.php
11+
identifier: shipmonk.deadMethod
612
paths:
713
- src
14+
- tests/unit

src/DocBlock/StandardTagFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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;
@@ -151,6 +152,7 @@ public static function createInstance(FqsenResolver $fqsenResolver): self
151152
new PropertyReadFactory($typeResolver, $descriptionFactory),
152153
new PropertyWriteFactory($typeResolver, $descriptionFactory),
153154
new MethodFactory($typeResolver, $descriptionFactory),
155+
new MixinFactory($typeResolver, $descriptionFactory),
154156
new ImplementsFactory($typeResolver, $descriptionFactory),
155157
new ExtendsFactory($typeResolver, $descriptionFactory),
156158
new TemplateFactory($typeResolver, $descriptionFactory),

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/MethodParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __toString(): string
8484
'$' . $this->getName() .
8585
(
8686
$this->defaultValue !== self::NO_DEFAULT_VALUE ?
87-
(new MethodParameterFactory())->format($this->defaultValue) :
87+
' = ' . (new MethodParameterFactory())->format($this->defaultValue) :
8888
''
8989
);
9090
}

src/DocBlock/Tags/TagWithType.php

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@
1313

1414
namespace phpDocumentor\Reflection\DocBlock\Tags;
1515

16-
use InvalidArgumentException;
1716
use phpDocumentor\Reflection\DocBlock\Tag;
1817
use phpDocumentor\Reflection\Exception\CannotCreateTag;
1918
use phpDocumentor\Reflection\Type;
2019

21-
use function in_array;
22-
use function sprintf;
23-
use function strlen;
24-
use function substr;
25-
use function trim;
26-
2720
abstract class TagWithType extends BaseTag
2821
{
2922
/** @var ?Type */
@@ -42,43 +35,6 @@ final public static function create(string $body): Tag
4235
throw new CannotCreateTag('Typed tag cannot be created');
4336
}
4437

45-
/**
46-
* @return string[]
47-
*/
48-
protected static function extractTypeFromBody(string $body): array
49-
{
50-
$type = '';
51-
$nestingLevel = 0;
52-
for ($i = 0, $iMax = strlen($body); $i < $iMax; $i++) {
53-
$character = $body[$i];
54-
55-
if ($nestingLevel === 0 && trim($character) === '') {
56-
break;
57-
}
58-
59-
$type .= $character;
60-
if (in_array($character, ['<', '(', '[', '{'])) {
61-
$nestingLevel++;
62-
continue;
63-
}
64-
65-
if (in_array($character, ['>', ')', ']', '}'])) {
66-
$nestingLevel--;
67-
continue;
68-
}
69-
}
70-
71-
if ($nestingLevel < 0 || $nestingLevel > 0) {
72-
throw new InvalidArgumentException(
73-
sprintf('Could not find type in %s, please check for malformed notations', $body)
74-
);
75-
}
76-
77-
$description = trim(substr($body, strlen($type)));
78-
79-
return [$type, $description];
80-
}
81-
8238
public function __toString(): string
8339
{
8440
if ($this->description) {

src/DocBlock/Tags/Template.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace phpDocumentor\Reflection\DocBlock\Tags;
1515

16-
use Doctrine\Deprecations\Deprecation;
1716
use phpDocumentor\Reflection\DocBlock\Description;
1817
use phpDocumentor\Reflection\DocBlock\Tag;
18+
use phpDocumentor\Reflection\Exception\CannotCreateTag;
1919
use phpDocumentor\Reflection\Type;
2020

2121
/**
@@ -51,14 +51,7 @@ public function __construct(
5151
*/
5252
public static function create(string $body): ?Tag
5353
{
54-
Deprecation::trigger(
55-
'phpdocumentor/reflection-docblock',
56-
'https://github.com/phpDocumentor/ReflectionDocBlock/issues/361',
57-
'Create using static factory is deprecated, this method should not be called directly
58-
by library consumers',
59-
);
60-
61-
return null;
54+
throw new CannotCreateTag('Template tag cannot be created');
6255
}
6356

6457
public function getTemplateName(): string

tests/unit/Assets/CustomParam.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010

1111
final class CustomParam implements Tag
1212
{
13+
/** @var string|null */
1314
public $myParam;
15+
16+
/** @var FqsenResolver|null */
1417
public $fqsenResolver;
1518

1619
public function getName() : string
1720
{
1821
return 'spy';
1922
}
2023

21-
public static function create($body, FqsenResolver $fqsenResolver = null, ?string $myParam = null)
24+
public static function create(string $body, FqsenResolver $fqsenResolver = null, ?string $myParam = null)
2225
{
2326
$tag = new self();
2427
$tag->fqsenResolver = $fqsenResolver;

tests/unit/Assets/CustomServiceClass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
use phpDocumentor\Reflection\DocBlock\Tag;
88
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
99
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
10-
use phpDocumentor\Reflection\FqsenResolver;
11-
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
1210

1311
final class CustomServiceClass implements Tag
1412
{
13+
/** @var Formatter|null */
1514
public $formatter;
1615

1716
public function getName() : string
1817
{
1918
return 'spy';
2019
}
2120

22-
public static function create($body, PassthroughFormatter $formatter = null)
21+
public static function create(string $body, PassthroughFormatter $formatter = null)
2322
{
2423
$tag = new self();
2524
$tag->formatter = $formatter;

0 commit comments

Comments
 (0)