Skip to content

Commit 4725bb9

Browse files
authored
Merge branch 'v5' into add_unique_validation
2 parents 71ab2f7 + 821ed96 commit 4725bb9

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

src/Extracting/MethodAstParser.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Exception;
66
use PhpParser\Node;
77
use PhpParser\NodeFinder;
8+
use PhpParser\NodeTraverser;
9+
use PhpParser\NodeVisitor\NameResolver;
810
use PhpParser\ParserFactory;
911
use ReflectionFunctionAbstract;
1012
use Throwable;
@@ -50,6 +52,13 @@ protected static function parseClassSourceCode(string $sourceCode): ?array
5052
throw new Exception("Parse error: {$error->getMessage()}");
5153
}
5254

55+
$traverser = new NodeTraverser(new NameResolver(options: ['replaceNodes' => false]));
56+
try {
57+
$traverser->traverse($ast);
58+
} catch (Throwable $error) {
59+
throw new Exception("Traverse error: {$error->getMessage()}");
60+
}
61+
5362
return $ast;
5463
}
5564

src/Extracting/Shared/ValidationRulesFinders/ValidatorMake.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Knuckles\Scribe\Extracting\Shared\ValidationRulesFinders;
44

55
use PhpParser\Node;
6+
use PhpParser\NodeFinder;
67

78
/**
89
* This class looks for
910
* $validator = Validator::make($request, ...)
11+
* Validator::make($request, ...)->validate()
1012
*
1113
* The variable names (`$validator` and `$request`) don't matter.
1214
*/
@@ -15,20 +17,19 @@ class ValidatorMake
1517
public static function find(Node $node)
1618
{
1719
// Make sure it's an assignment
18-
if (!($node instanceof Node\Stmt\Expression)
19-
|| !($node->expr instanceof Node\Expr\Assign)) {
20+
if (! ($node instanceof Node\Stmt\Expression)) {
2021
return;
2122
}
2223

23-
$expr = $node->expr->expr; // Get the expression on the RHS
24+
$validatorNode = (new NodeFinder)->findFirst($node, function ($node): bool {
25+
return $node instanceof Node\Expr\StaticCall
26+
&& ! empty($node->class->name)
27+
&& str_ends_with($node->class->name, 'Validator')
28+
&& $node->name->name == 'make';
29+
});
2430

25-
if (
26-
$expr instanceof Node\Expr\StaticCall
27-
&& !empty($expr->class->name)
28-
&& str_ends_with($expr->class->name, "Validator")
29-
&& $expr->name->name == "make"
30-
) {
31-
return $expr->args[1]->value;
31+
if ($validatorNode instanceof Node\Expr\StaticCall) {
32+
return $validatorNode->args[1]->value;
3233
}
3334
}
3435
}

src/Writing/CustomTranslationsLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public function load($locale, $group, $namespace = null)
3333
} elseif ($this->files->exists($full = "{$this->hints[$namespace]}/scribe.php")) {
3434
$this->scribeTranslationsCache = $this->files->getRequire($full);
3535
// getRequire() requires the Scribe file, which will return an array
36-
// @phpstan-ignore-next-line nullCoalesce.offset
3736
$lines = $this->scribeTranslationsCache[$group] ?? [];
3837
} else {
3938
return [];

tests/Fixtures/TestController.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,29 @@ public function withInlineValidatorMake(Request $request)
646646

647647
}
648648
}
649+
public function withInlineValidatorMakeValidate(Request $request)
650+
{
651+
// Some stuff
652+
Validator::make($request, [
653+
// The id of the user. Example: 9
654+
'user_id' => 'int|required',
655+
// The id of the room.
656+
'room_id' => ['string', 'in:3,5,6'],
657+
// Whether to ban the user forever. Example: false
658+
'forever' => 'boolean',
659+
// Just need something here. No-example
660+
'another_one' => 'numeric',
661+
'even_more_param' => 'array',
662+
'book.name' => 'string',
663+
'book.author_id' => 'integer',
664+
'book.pages_count' => 'integer',
665+
'ids.*' => 'integer',
666+
// The first name of the user. Example: John
667+
'users.*.first_name' => ['string'],
668+
// The last name of the user. Example: Doe
669+
'users.*.last_name' => 'string',
670+
])->validate();
671+
}
649672

650673
public function withInlineRequestValidateWithBag(Request $request)
651674
{

tests/Strategies/GetFromInlineValidatorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ public function can_fetch_from_validator_make()
208208
$this->assertIsArray($results['ids']['example']);
209209
}
210210

211+
/** @test */
212+
public function can_fetch_from_validator_make_validate()
213+
{
214+
$endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
215+
$e->method = new \ReflectionMethod(TestController::class, 'withInlineValidatorMakeValidate');
216+
});
217+
218+
$results = $this->fetchViaBodyParams($endpoint);
219+
220+
$this->assertArraySubset(self::$expected, $results);
221+
$this->assertIsArray($results['ids']['example']);
222+
}
223+
211224
/** @test */
212225
public function respects_query_params_comment()
213226
{

0 commit comments

Comments
 (0)