Skip to content

Commit

Permalink
Support php-parser v5
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Feb 20, 2024
1 parent 5281340 commit b5da6b8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions composer.dingo.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"illuminate/support": "^8.0|^9.0",
"league/flysystem": "^1.1.4|^2.1.1|^3.0",
"mpociot/reflection-docblock": "^1.0.1",
"nikic/php-parser": "^4.10",
"nunomaduro/collision": "^5.10|^6.0",
"nikic/php-parser": "^5.0",
"nunomaduro/collision": "^5.10|^6.0|^7.0|^8.0",
"ramsey/uuid": "^4.2.2",
"shalvah/clara": "^3.1.0",
"shalvah/upgrader": "0.*",
"shalvah/upgrader": ">=0.6.0",
"spatie/data-transfer-object": "^2.6|^3.0",
"symfony/var-exporter": "^5.4|^6.0|^7.0",
"symfony/yaml": "^5.4|^6.0|^7.0"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"illuminate/support": "^8.0|^9.0|^10.0",
"league/flysystem": "^1.1.4|^2.1.1|^3.0",
"mpociot/reflection-docblock": "^1.0.1",
"nikic/php-parser": "^4.10",
"nikic/php-parser": "^5.0",
"nunomaduro/collision": "^5.10|^6.0|^7.0|^8.0",
"ramsey/uuid": "^4.2.2",
"shalvah/clara": "^3.1.0",
"shalvah/upgrader": "^0.3.0",
"shalvah/upgrader": ">=0.6.0",
"spatie/data-transfer-object": "^2.6|^3.0",
"symfony/var-exporter": "^5.4|^6.0|^7.0",
"symfony/yaml": "^5.4|^6.0|^7.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Extracting/MethodAstParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function getMethodAst(ReflectionFunctionAbstract $method)
*/
protected static function parseClassSourceCode(string $sourceCode): ?array
{
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory)->createForHostVersion();
try {
$ast = $parser->parse($sourceCode);
} catch (Throwable $error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static function find(Node $node)

if (
$expr instanceof Node\Expr\StaticCall
&& !empty($expr->class->parts)
&& end($expr->class->parts) == "Validator"
&& !empty($expr->class->name)
&& str_ends_with($expr->class->name, "Validator")
&& $expr->name->name == "make"
) {
return $expr->args[1]->value;
Expand Down
12 changes: 6 additions & 6 deletions src/Extracting/Strategies/GetFromInlineValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function lookForInlineValidationRules(ClassMethod $methodAst): array
$rules = [];
$customParameterData = [];
foreach ($validationRules->items as $item) {
/** @var Node\Expr\ArrayItem $item */
/** @var Node\ArrayItem $item */
if (!$item->key instanceof Node\Scalar\String_) {
continue;
}
Expand All @@ -77,7 +77,7 @@ public function lookForInlineValidationRules(ClassMethod $methodAst): array
} else if ($item->value instanceof Node\Expr\Array_) {
$rulesList = [];
foreach ($item->value->items as $arrayItem) {
/** @var Node\Expr\ArrayItem $arrayItem */
/** @var Node\ArrayItem $arrayItem */
if ($arrayItem->value instanceof Node\Scalar\String_) {
$rulesList[] = $arrayItem->value->value;
}
Expand Down Expand Up @@ -122,22 +122,22 @@ enum_exists($enum) && method_exists($enum, 'tryFrom')
return [$rules, $customParameterData];
}

protected function extractEnumClassFromArrayItem(Node\Expr\ArrayItem $arrayItem): ?string
protected function extractEnumClassFromArrayItem(Node\ArrayItem $arrayItem): ?string
{
$args = [];

// Enum rule with the form "new Enum(...)"
if ($arrayItem->value instanceof Node\Expr\New_ &&
$arrayItem->value->class instanceof Node\Name &&
last($arrayItem->value->class->parts) === 'Enum'
str_ends_with($arrayItem->value->class->name, 'Enum')
) {
$args = $arrayItem->value->args;
}

// Enum rule with the form "Rule::enum(...)"
else if ($arrayItem->value instanceof Node\Expr\StaticCall &&
$arrayItem->value->class instanceof Node\Name &&
last($arrayItem->value->class->parts) === 'Rule' &&
str_ends_with($arrayItem->value->class->name, 'Rule') &&
$arrayItem->value->name instanceof Node\Identifier &&
$arrayItem->value->name->name === 'enum'
) {
Expand All @@ -150,7 +150,7 @@ protected function extractEnumClassFromArrayItem(Node\Expr\ArrayItem $arrayItem)
if ($arg->value instanceof Node\Expr\ClassConstFetch &&
$arg->value->class instanceof Node\Name
) {
return '\\' . implode('\\', $arg->value->class->parts);
return '\\' . $arg->value->class->name;
} else if ($arg->value instanceof Node\Scalar\String_) {
return $arg->value->value;
}
Expand Down

0 comments on commit b5da6b8

Please sign in to comment.