-
-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support parsing `$request->validate(...)` without assignment, and `$this->validate($request, ...)`
- Loading branch information
Showing
8 changed files
with
281 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Extracting\ValidationRulesFinders; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* This class looks for | ||
* $anyVariable = $request->validate(...); | ||
* or just | ||
* $request->validate(...); | ||
* | ||
* Also supports `$req` instead of `$request` | ||
* Also supports `->validateWithBag('', ...)` | ||
*/ | ||
class RequestValidate | ||
{ | ||
public static function find(Node $node) | ||
{ | ||
if (!($node instanceof Node\Stmt\Expression)) return; | ||
|
||
$expr = $node->expr; | ||
if ($expr instanceof Node\Expr\Assign) { | ||
$expr = $expr->expr; // If it's an assignment, get the expression on the RHS | ||
} | ||
|
||
if ( | ||
$expr instanceof Node\Expr\MethodCall | ||
&& $expr->var instanceof Node\Expr\Variable | ||
&& in_array($expr->var->name, ["request", "req"]) | ||
) { | ||
if ($expr->name->name == "validate") { | ||
return $expr->args[0]->value; | ||
} | ||
|
||
if ($expr->name->name == "validateWithBag") { | ||
return $expr->args[1]->value; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Extracting\ValidationRulesFinders; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* This class looks for | ||
* $anyVariable = $this->validate($request, ...); | ||
* or just | ||
* $this->validate($request, ...); | ||
* | ||
* Also supports `$req` instead of `$request` | ||
*/ | ||
class ThisValidate | ||
{ | ||
public static function find(Node $node) | ||
{ | ||
if (!($node instanceof Node\Stmt\Expression)) return; | ||
|
||
$expr = $node->expr; | ||
if ($expr instanceof Node\Expr\Assign) { | ||
$expr = $expr->expr; // If it's an assignment, get the expression on the RHS | ||
} | ||
|
||
if ( | ||
$expr instanceof Node\Expr\MethodCall | ||
&& $expr->var instanceof Node\Expr\Variable | ||
&& $expr->var->name === "this" | ||
) { | ||
if ($expr->name->name == "validate") { | ||
return $expr->args[1]->value; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Extracting\ValidationRulesFinders; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* This class looks for | ||
* $validator = Validator::make($request, ...) | ||
* | ||
* The variable names (`$validator` and `$request`) don't matter. | ||
*/ | ||
class ValidatorMake | ||
{ | ||
public static function find(Node $node) | ||
{ | ||
// Make sure it's an assignment | ||
if (!($node instanceof Node\Stmt\Expression) | ||
|| !($node->expr instanceof Node\Expr\Assign)) { | ||
return; | ||
} | ||
|
||
$expr = $node->expr->expr; // Get the expression on the RHS | ||
|
||
if ( | ||
$expr instanceof Node\Expr\StaticCall | ||
&& !empty($expr->class->parts) | ||
&& end($expr->class->parts) == "Validator" | ||
&& $expr->name->name == "make" | ||
) { | ||
return $expr->args[1]->value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters