Skip to content

Commit 9d7e5e6

Browse files
committed
[+]: "PHPParameter" -> catch UnableToCompileNode Exceptions
1 parent e9800f0 commit 9d7e5e6

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/voku/SimplePhpParser/Model/PHPParameter.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ public function readObjectFromBetterReflection($parameter): self
134134
$this->name = $parameter->getName();
135135

136136
if ($parameter->isDefaultValueAvailable()) {
137-
$this->defaultValue = $parameter->getDefaultValue();
138-
137+
try {
138+
$this->defaultValue = $parameter->getDefaultValue();
139+
} catch (\Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode $e) {
140+
// nothing
141+
}
139142
if ($this->defaultValue !== null) {
140143
$this->typeFromDefaultValue = Utils::normalizePhpType(\gettype($this->defaultValue));
141144
}
@@ -152,7 +155,11 @@ public function readObjectFromBetterReflection($parameter): self
152155
$this->readPhpDoc($docComment, $this->name);
153156
}
154157

155-
$type = $parameter->getType();
158+
try {
159+
$type = $parameter->getType();
160+
} catch (\Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode $e) {
161+
$type = null;
162+
}
156163
if ($type !== null) {
157164
if (\method_exists($type, 'getName')) {
158165
$this->type = Utils::normalizePhpType($type->getName());

src/voku/SimplePhpParser/Parsers/Helper/Utils.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ public static function getPhpParserValueFromNode(
159159
return null;
160160
}
161161

162-
return $node->name->parts[0];
162+
if (\defined($node->name->parts[0])) {
163+
return \constant($node->name->parts[0]);
164+
}
163165
}
164166

165167
return self::GET_PHP_PARSER_VALUE_FROM_NODE_HELPER;

tests/CheckerTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ public function testCheckPhpClasses(): void
2020
static::assertSame(
2121
[
2222
'Simple-PHP-Code-Parser/tests/Dummy3.php' => [
23-
0 => '[7]: missing return type for voku\tests\foo3()',
24-
1 => '[15]: missing property type for voku\tests\Dummy3->$foo',
25-
2 => '[15]: missing property type for voku\tests\Dummy3->$foo_mixed',
26-
3 => '[150]: missing parameter type "null" in phpdoc from voku\tests\Dummy3->lall8() | parameter:case',
27-
4 => '[40]: missing parameter type for voku\tests\Dummy3->lall() | parameter:foo',
28-
5 => '[40]: missing return type for voku\tests\Dummy3->lall()',
29-
6 => '[70]: wrong return type "string" in phpdoc from voku\tests\Dummy3->lall3()',
30-
7 => '[60]: wrong return type "null" in phpdoc from voku\tests\Dummy3->lall2_1()',
31-
8 => '[50]: missing return type "null" in phpdoc from voku\tests\Dummy3->lall2()',
32-
9 => '[80]: wrong parameter type "string" in phpdoc from voku\tests\Dummy3->lall3_1() | parameter:foo',
23+
0 => '[9]: missing return type for voku\tests\foo3()',
24+
1 => '[19]: missing property type for voku\tests\Dummy3->$foo',
25+
2 => '[19]: missing property type for voku\tests\Dummy3->$foo_mixed',
26+
3 => '[154]: missing parameter type "null" in phpdoc from voku\tests\Dummy3->lall8() | parameter:case',
27+
4 => '[44]: missing parameter type for voku\tests\Dummy3->lall() | parameter:foo',
28+
5 => '[44]: missing return type for voku\tests\Dummy3->lall()',
29+
6 => '[74]: wrong return type "string" in phpdoc from voku\tests\Dummy3->lall3()',
30+
7 => '[64]: wrong return type "null" in phpdoc from voku\tests\Dummy3->lall2_1()',
31+
8 => '[54]: missing return type "null" in phpdoc from voku\tests\Dummy3->lall2()',
32+
9 => '[84]: wrong parameter type "string" in phpdoc from voku\tests\Dummy3->lall3_1() | parameter:foo',
3333
],
3434
],
3535
$phpCodeErrors

tests/Dummy3.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace voku\tests;
66

7+
use const SORT_ASC as SORT_ASC_TEST;
8+
79
function foo3(int $foo = 0)
810
{
911
return new Dummy();
1012
}
1113

14+
const SORT_ASC_TEST_2 = SORT_ASC;
15+
1216
/**
1317
* @internal
1418
*/
@@ -162,6 +166,26 @@ public function lall9(int $case = self::CASE_NULL): DummyInterface
162166
return new self;
163167
}
164168

169+
/**
170+
* @param int $case
171+
*
172+
* @return \voku\tests\Dummy3
173+
*/
174+
public function lall10(int $case = SORT_ASC_TEST): DummyInterface
175+
{
176+
return new self;
177+
}
178+
179+
/**
180+
* @param int $case
181+
*
182+
* @return \voku\tests\Dummy3
183+
*/
184+
public function lall11(int $case = SORT_ASC_TEST_2): DummyInterface
185+
{
186+
return new self;
187+
}
188+
165189
/**
166190
* This is a test-text [...] öäü !"§?.
167191
*

0 commit comments

Comments
 (0)