Skip to content

Commit b5b4447

Browse files
committed
[+]: "PhpCodeParser" -> ignore errors outside the current file-path-scope
-> fix more inheritdoc errors -> rename ".*MaybeWithComment" -> ".*FromPhpDocMaybeWithComment"
1 parent 679d4ed commit b5b4447

22 files changed

+598
-407
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"voku/simple-cache": "~4.0",
2828
"nikic/php-parser": "~4.4",
2929
"ondrejmirtes/better-reflection": "~4.3",
30-
"vimeo/psalm": "~3.11"
30+
"vimeo/psalm": "~3.11",
31+
"symfony/console": "~3.4 || ~4.1 || ~5.0"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "~6.0 || ~7.0"

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ parameters:
88
excludes_analyse:
99
- %currentWorkingDirectory%/vendor/*
1010
- %currentWorkingDirectory%/tests/*
11-
autoload_files:
12-
- %currentWorkingDirectory%/vendor/autoload.php
1311
ignoreErrors:
1412
- '#should be compatible with#'
1513
- '#Argument of an invalid type voku\\SimplePhpParser\\Model\\PHP.* supplied for foreach, only iterables are supported\.#'
1614
- '#undefined property PhpParser\\Node::\$name#'
1715
- '#undefined property PhpParser\\Node\\Expr#'
18-
- '#Utils::parseDocTypeObject\(\) should return array#'
1916
- '#BasePHPElement::getFQN\(\) should return class-string but returns string#'
2017
- '#Unable to resolve the template type .* in call to function Amp\\Promise\\.*#'

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
<issueHandlers>
1717
<InternalClass errorLevel="suppress" />
18+
<MoreSpecificReturnType errorLevel="suppress" />
19+
<LessSpecificReturnStatement errorLevel="suppress" />
1820
<MoreSpecificImplementedParamType errorLevel="suppress" />
21+
<UnresolvableInclude errorLevel="suppress" />
1922
</issueHandlers>
2023

2124
</psalm>

src/voku/SimplePhpParser/CliCommand/PhpCodeCheckerCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public function execute(InputInterface $input, OutputInterface $output): int
103103
\assert(\is_string($access));
104104
$access = (array) \explode('|', $access);
105105

106-
$skipMixedTypesAsError = (bool) $input->getOption('skip-mixed-types-as-error');
107-
$skipDeprecatedFunctions = (bool) $input->getOption('skip-deprecated-functions');
108-
$skipFunctionsWithLeadingUnderscore = (bool) $input->getOption('skip-functions-with-leading-underscore');
109-
$skipParseErrorsAsError = (bool) $input->getOption('skip-parse-errors');
106+
$skipMixedTypesAsError = $input->getOption('skip-mixed-types-as-error') !== 'false';
107+
$skipDeprecatedFunctions = $input->getOption('skip-deprecated-functions') !== 'false';
108+
$skipFunctionsWithLeadingUnderscore = $input->getOption('skip-functions-with-leading-underscore') !== 'false';
109+
$skipParseErrorsAsError = $input->getOption('skip-parse-errors') !== 'false';
110110

111111
$formatter = $output->getFormatter();
112112
$formatter->setStyle('file', new OutputFormatterStyle('default', null, ['bold']));

src/voku/SimplePhpParser/CliCommand/PhpCodeDumpApi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ public function execute(InputInterface $input, OutputInterface $output): int
7979
$output->writeln('');
8080

8181
foreach ($phpParser->getClasses() as $class) {
82+
/** @noinspection DisconnectedForeachInstructionInspection */
8283
$output->writeln('');
83-
$output->writeln(\sprintf('<class>%s</class>', $class->name));
84+
$output->writeln(\sprintf('<class>%s</class>', $class->name ?? '?'));
8485

8586
foreach ($class->methods as $method) {
8687
if ($method->access !== 'public') {

src/voku/SimplePhpParser/Model/BasePHPElement.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
8282
* @return string
8383
*
8484
* @psalm-return class-string
85-
*
86-
* @psalm-suppress MoreSpecificReturnType or Less ?
8785
*/
8886
protected static function getFQN($node): string
8987
{
@@ -105,7 +103,6 @@ protected static function getFQN($node): string
105103
}
106104
}
107105

108-
/** @psalm-suppress LessSpecificReturnStatement ? */
109106
return \rtrim($fqn, '\\');
110107
}
111108

src/voku/SimplePhpParser/Model/PHPClass.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
class PHPClass extends BasePHPClass
1212
{
1313
/**
14-
* @var string
14+
* @var string|null
1515
*
16-
* @psalm-var class-string
16+
* @psalm-var null|class-string
1717
*/
1818
public $name;
1919

@@ -130,6 +130,17 @@ public function readObjectFromBetterReflection($clazz): self
130130
$parent = $clazz->getParentClass();
131131
if ($parent) {
132132
$this->parentClass = $parent->getName();
133+
134+
/** @noinspection ArgumentEqualsDefaultValueInspection */
135+
if (
136+
!$this->parserContainer->getClass($this->parentClass)
137+
&&
138+
\class_exists($this->parentClass, true)
139+
) {
140+
$reflectionClass = ReflectionClass::createFromName($this->parentClass);
141+
$class = (new self($this->parserContainer))->readObjectFromBetterReflection($reflectionClass);
142+
$this->parserContainer->addClass($class);
143+
}
133144
}
134145

135146
foreach ($clazz->getProperties() as $property) {
@@ -173,7 +184,7 @@ public function readObjectFromBetterReflection($clazz): self
173184
*
174185
* @return array
175186
*
176-
* @psalm-return array<string, array{type: null|string, typeMaybeWithComment: null|string, typeFromPhpDoc: null|string, typeFromPhpDocSimple: null|string, typeFromPhpDocPslam: null|string, typeFromDefaultValue: null|string}>
187+
* @psalm-return array<string, array{type: null|string, typeFromPhpDocMaybeWithComment: null|string, typeFromPhpDoc: null|string, typeFromPhpDocSimple: null|string, typeFromPhpDocPslam: null|string, typeFromDefaultValue: null|string}>
177188
*/
178189
public function getPropertiesInfo(
179190
array $access = ['public', 'protected', 'private'],
@@ -193,7 +204,7 @@ public function getPropertiesInfo(
193204

194205
$types = [];
195206
$types['type'] = $property->type;
196-
$types['typeMaybeWithComment'] = $property->typeMaybeWithComment;
207+
$types['typeFromPhpDocMaybeWithComment'] = $property->typeFromPhpDocMaybeWithComment;
197208
$types['typeFromPhpDoc'] = $property->typeFromPhpDoc;
198209
$types['typeFromPhpDocSimple'] = $property->typeFromPhpDocSimple;
199210
$types['typeFromPhpDocPslam'] = $property->typeFromPhpDocPslam;
@@ -212,9 +223,7 @@ public function getPropertiesInfo(
212223
*
213224
* @return array<mixed>
214225
*
215-
* @psalm-return array<string, array{fullDescription: string, line: null|int, file: null|string, error: string, is_deprecated: bool, is_static: bool, is_meta: bool, is_internal: bool, is_removed: bool, paramsTypes: array<string, array{type: null|string, typeFromPhpDoc: null|string, typeFromPhpDocPslam: null|string, typeFromPhpDocSimple: null|string, typeMaybeWithComment: null|string, typeFromDefaultValue: null|string}>, returnTypes: array{type: null|string, typeFromPhpDoc: null|string, typeFromPhpDocPslam: null|string, typeFromPhpDocSimple: null|string, typeMaybeWithComment: null|string}}>
216-
*
217-
* @psalm-suppress MoreSpecificReturnType or Less ?
226+
* @psalm-return array<string, array{fullDescription: string, line: null|int, file: null|string, error: string, is_deprecated: bool, is_static: null|bool, is_meta: bool, is_internal: bool, is_removed: bool, paramsTypes: array<string, array{type: null|string, typeFromPhpDoc: null|string, typeFromPhpDocPslam: null|string, typeFromPhpDocSimple: null|string, typeFromPhpDocMaybeWithComment: null|string, typeFromDefaultValue: null|string}>, returnTypes: array{type: null|string, typeFromPhpDoc: null|string, typeFromPhpDocPslam: null|string, typeFromPhpDocSimple: null|string, typeFromPhpDocMaybeWithComment: null|string}}>
218227
*/
219228
public function getMethodsInfo(
220229
array $access = ['public', 'protected', 'private'],
@@ -240,7 +249,7 @@ public function getMethodsInfo(
240249
$paramsTypes = [];
241250
foreach ($method->parameters as $tagParam) {
242251
$paramsTypes[$tagParam->name]['type'] = $tagParam->type;
243-
$paramsTypes[$tagParam->name]['typeMaybeWithComment'] = $tagParam->typeMaybeWithComment;
252+
$paramsTypes[$tagParam->name]['typeFromPhpDocMaybeWithComment'] = $tagParam->typeFromPhpDocMaybeWithComment;
244253
$paramsTypes[$tagParam->name]['typeFromPhpDoc'] = $tagParam->typeFromPhpDoc;
245254
$paramsTypes[$tagParam->name]['typeFromPhpDocSimple'] = $tagParam->typeFromPhpDocSimple;
246255
$paramsTypes[$tagParam->name]['typeFromPhpDocPslam'] = $tagParam->typeFromPhpDocPslam;
@@ -249,7 +258,7 @@ public function getMethodsInfo(
249258

250259
$returnTypes = [];
251260
$returnTypes['type'] = $method->returnType;
252-
$returnTypes['typeMaybeWithComment'] = $method->returnTypeMaybeWithComment;
261+
$returnTypes['typeFromPhpDocMaybeWithComment'] = $method->returnTypeFromPhpDocMaybeWithComment;
253262
$returnTypes['typeFromPhpDoc'] = $method->returnTypeFromPhpDoc;
254263
$returnTypes['typeFromPhpDocSimple'] = $method->returnTypeFromPhpDocSimple;
255264
$returnTypes['typeFromPhpDocPslam'] = $method->returnTypeFromPhpDocPslam;
@@ -275,7 +284,6 @@ public function getMethodsInfo(
275284

276285
\asort($allInfo);
277286

278-
/** @psalm-suppress LessSpecificReturnStatement ? */
279287
return $allInfo;
280288
}
281289

@@ -322,13 +330,13 @@ private function readPhpDocProperties(string $docComment): void
322330

323331
$propertyPhp->typeFromPhpDoc = Utils::normalizePhpType($type . '');
324332

325-
$typeMaybeWithCommentTmp = \trim((string) $parsedPropertyTag);
333+
$typeFromPhpDocMaybeWithCommentTmp = \trim((string) $parsedPropertyTag);
326334
if (
327-
$typeMaybeWithCommentTmp
335+
$typeFromPhpDocMaybeWithCommentTmp
328336
&&
329-
\strpos($typeMaybeWithCommentTmp, '$') !== 0
337+
\strpos($typeFromPhpDocMaybeWithCommentTmp, '$') !== 0
330338
) {
331-
$propertyPhp->typeMaybeWithComment = $typeMaybeWithCommentTmp;
339+
$propertyPhp->typeFromPhpDocMaybeWithComment = $typeFromPhpDocMaybeWithCommentTmp;
332340
}
333341

334342
$typeTmp = Utils::parseDocTypeObject($type);
@@ -346,7 +354,7 @@ private function readPhpDocProperties(string $docComment): void
346354
}
347355
}
348356
} catch (\Exception $e) {
349-
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '') . ' | ' . \print_r($e->getMessage(), true);
357+
$tmpErrorMessage = ($this->name ?? '?') . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
350358
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
351359
}
352360
}

src/voku/SimplePhpParser/Model/PHPDefineConstant.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function readObjectFromPhpNode($node, $dummy = null): PHPConst
2626
\property_exists($node->args[0], 'value')
2727
&&
2828
\property_exists($node->args[0]->value, 'value')
29+
&&
30+
$node->args[0]->value instanceof \PhpParser\Node\Scalar\String_
2931
) {
3032
$constName = (string) $node->args[0]->value->value;
3133
}

src/voku/SimplePhpParser/Model/PHPDocElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ trait PHPDocElement
9292
protected function collectTags(Node $node): void
9393
{
9494
$docComment = $node->getDocComment();
95-
if ($docComment !== null) {
95+
if ($docComment) {
9696
try {
9797
$phpDoc = DocFactoryProvider::getDocFactory()->create($docComment->getText());
9898

@@ -117,7 +117,7 @@ protected function collectTags(Node $node): void
117117
$this->hasInternalTag = $phpDoc->hasTag('internal');
118118
$this->hasRemovedTag = $phpDoc->hasTag('removed');
119119
} catch (\Exception $e) {
120-
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '') . ' | ' . \print_r($e->getMessage(), true);
120+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
121121
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
122122
}
123123
}

src/voku/SimplePhpParser/Model/PHPFunction.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PHPFunction extends BasePHPElement
4242
/**
4343
* @var string|null
4444
*/
45-
public $returnTypeMaybeWithComment;
45+
public $returnTypeFromPhpDocMaybeWithComment;
4646

4747
/**
4848
* @var string
@@ -95,7 +95,7 @@ public function readObjectFromPhpNode($node, $dummy = null): self
9595
$this->summary = $phpDoc->getSummary();
9696
$this->description = (string) $phpDoc->getDescription();
9797
} catch (\Exception $e) {
98-
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '') . ' | ' . \print_r($e->getMessage(), true);
98+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
9999
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
100100
}
101101
}
@@ -113,9 +113,9 @@ public function readObjectFromPhpNode($node, $dummy = null): self
113113

114114
$this->collectTags($node);
115115

116-
$nodeDoc = $node->getDocComment();
117-
if ($nodeDoc) {
118-
$this->readPhpDoc($nodeDoc->getText());
116+
$docComment = $node->getDocComment();
117+
if ($docComment) {
118+
$this->readPhpDoc($docComment->getText());
119119
}
120120

121121
return $this;
@@ -157,7 +157,7 @@ public function readObjectFromBetterReflection($function): self
157157
}
158158

159159
$docComment = $function->getDocComment();
160-
if ($docComment !== null) {
160+
if ($docComment) {
161161
$this->readPhpDoc($docComment);
162162
}
163163

@@ -177,9 +177,9 @@ public function readObjectFromBetterReflection($function): self
177177
$this->parameters[$param->name] = $param;
178178
}
179179

180-
$docCommentText = $function->getDocComment();
181-
if ($docCommentText) {
182-
$this->readPhpDoc($docCommentText);
180+
$docComment = $function->getDocComment();
181+
if ($docComment) {
182+
$this->readPhpDoc($docComment);
183183
}
184184

185185
return $this;
@@ -214,7 +214,7 @@ protected function readPhpDoc(string $docComment): void
214214
try {
215215
$regexIntValues = '/@.*?return\s+(?<intValues>\d[\|\d]*)(?<comment>.*)/ui';
216216
if (\preg_match($regexIntValues, $docComment, $matchesIntValues)) {
217-
$this->returnTypeMaybeWithComment = 'int' . (\trim($matchesIntValues['comment']) ? ' ' . \trim($matchesIntValues['comment']) : '');
217+
$this->returnTypeFromPhpDocMaybeWithComment = 'int' . (\trim($matchesIntValues['comment']) ? ' ' . \trim($matchesIntValues['comment']) : '');
218218
$this->returnTypeFromPhpDoc = 'int';
219219
$this->returnTypeFromPhpDocSimple = 'int';
220220
$this->returnTypeFromPhpDocPslam = $matchesIntValues['intValues'];
@@ -224,7 +224,7 @@ protected function readPhpDoc(string $docComment): void
224224

225225
$regexAnd = '/@.*?return\s+(?<type>(?<type1>[\S]+)&(?<type2>[\S]+))(?<comment>.*)/ui';
226226
if (\preg_match($regexAnd, $docComment, $matchesAndValues)) {
227-
$this->returnTypeMaybeWithComment = $matchesAndValues['type'] . (\trim($matchesAndValues['comment']) ? ' ' . \trim($matchesAndValues['comment']) : '');
227+
$this->returnTypeFromPhpDocMaybeWithComment = $matchesAndValues['type'] . (\trim($matchesAndValues['comment']) ? ' ' . \trim($matchesAndValues['comment']) : '');
228228
$this->returnTypeFromPhpDoc = $matchesAndValues['type1'] . '|' . $matchesAndValues['type2'];
229229
$this->returnTypeFromPhpDocSimple = $matchesAndValues['type1'] . '|' . $matchesAndValues['type2'];
230230
$this->returnTypeFromPhpDocPslam = $matchesAndValues['type'];
@@ -240,7 +240,7 @@ protected function readPhpDoc(string $docComment): void
240240
/** @var Return_ $parsedReturnTagReturn */
241241
$parsedReturnTagReturn = $parsedReturnTag[0];
242242

243-
$this->returnTypeMaybeWithComment = \trim((string) $parsedReturnTagReturn);
243+
$this->returnTypeFromPhpDocMaybeWithComment = \trim((string) $parsedReturnTagReturn);
244244

245245
$type = $parsedReturnTagReturn->getType();
246246

@@ -268,7 +268,7 @@ protected function readPhpDoc(string $docComment): void
268268
$this->returnTypeFromPhpDocPslam = (string) @\Psalm\Type::parseString($parsedReturnTagReturn);
269269
}
270270
} catch (\Exception $e) {
271-
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '') . ' | ' . \print_r($e->getMessage(), true);
271+
$tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
272272
$this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
273273
}
274274
}

0 commit comments

Comments
 (0)