-
Notifications
You must be signed in to change notification settings - Fork 1
5&6 86c34dha8 clerk step #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RGO230
wants to merge
11
commits into
main
Choose a base branch
from
5&6-86c34dha8-clerk-step
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
24be61d
feat: Php parser and modify user model file after installed clerk aut…
RGO230 41d089b
style: fix
RGO230 7b4394f
style:fix
RGO230 88cf03b
refactor: make bind for parser
RGO230 ffd2950
tests: fix test fixture
RGO230 cfd70d2
Merge branch '4-86c34dha8-clerk-step' into 5&6-86c34dha8-clerk-step
RGO230 8da124c
fix: add value visitor
RGO230 eca70ea
fix: fixture
RGO230 361a5a4
fix: target item check
RGO230 93cdb28
Merge branch 'main' into 5&6-86c34dha8-clerk-step
RGO230 52838bb
Merge branch 'main' into 5&6-86c34dha8-clerk-step
RGO230 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Factories; | ||
|
||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use RonasIT\ProjectInitializator\Support\Parser\PhpParser; | ||
|
||
class PhpParserFactory | ||
{ | ||
public static function create(string $filePath): PhpParser | ||
{ | ||
return new PhpParser( | ||
$filePath, | ||
(new ParserFactory())->createForNewestSupportedVersion(), | ||
new NodeTraverser(), | ||
new Standard(), | ||
); | ||
} | ||
} |
This file contains hidden or 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,118 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser; | ||
|
||
use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors\RemoveValueFromArrayPropertyPropertyArrayVisitor; | ||
use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\MethodReturnArrayVisitors\RemoveValueFromMethodReturnArrayVisitor; | ||
use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors\AddValueToArrayPropertyPropertyArrayVisitor; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PhpParser\Node\Stmt\Nop; | ||
use PhpParser\Node\Stmt\Use_; | ||
use PhpParser\Parser; | ||
use PhpParser\PrettyPrinterAbstract; | ||
use PhpParser\NodeTraverserInterface; | ||
|
||
class PhpParser | ||
{ | ||
private array $ast; | ||
|
||
public function __construct( | ||
protected string $filePath, | ||
protected Parser $parser, | ||
protected NodeTraverserInterface $traverser, | ||
protected PrettyPrinterAbstract $printer, | ||
) | ||
{ | ||
$this->ast = $parser->parse(file_get_contents($filePath)); | ||
|
||
$this->addEmptySpacesVisitor(); | ||
} | ||
|
||
public function removeValueFromArrayProperty(array $propertyNames, string $value): self | ||
{ | ||
$this->traverser->addVisitor(new RemoveValueFromArrayPropertyPropertyArrayVisitor($propertyNames, $value)); | ||
|
||
return $this; | ||
} | ||
|
||
public function removeValueFromMethodReturnArray(array $methodNames, string $value): self | ||
{ | ||
$this->traverser->addVisitor(new RemoveValueFromMethodReturnArrayVisitor($methodNames, $value)); | ||
|
||
return $this; | ||
} | ||
|
||
public function addValueToArrayProperty(array $propertyNames, string $value): self | ||
{ | ||
$this->traverser->addVisitor(new AddValueToArrayPropertyPropertyArrayVisitor($propertyNames, $value)); | ||
|
||
return $this; | ||
} | ||
|
||
public function save(): void | ||
{ | ||
$modifiedAst = $this->traverser->traverse($this->ast); | ||
|
||
file_put_contents($this->filePath, $this->printer->prettyPrintFile($modifiedAst)); | ||
} | ||
|
||
protected function addEmptySpacesVisitor(): void | ||
{ | ||
$this->traverser->addVisitor( | ||
new class extends NodeVisitorAbstract { | ||
public function enterNode(Node $node): void | ||
{ | ||
if (!$node instanceof Namespace_) { | ||
return; | ||
} | ||
|
||
$this->addLineAfterLastUse($node); | ||
$this->addEndLine($node); | ||
} | ||
|
||
private function addLineAfterLastUse(Namespace_ $namespace): void | ||
{ | ||
$lastUseIndex = $this->findLastUseIndex($namespace); | ||
|
||
if ($lastUseIndex === null) { | ||
return; | ||
} | ||
|
||
$afterLastUse = $namespace->stmts[$lastUseIndex + 1] ?? null; | ||
|
||
if (!$afterLastUse instanceof Nop) { | ||
array_splice($namespace->stmts, $lastUseIndex + 1, 0, [new Nop()]); | ||
} | ||
} | ||
|
||
private function addEndLine(Namespace_ $namespace): void | ||
{ | ||
$lastStmtIndex = count($namespace->stmts) - 1; | ||
|
||
if ($lastStmtIndex < 0) { | ||
return; | ||
} | ||
|
||
if (!$namespace->stmts[$lastStmtIndex] instanceof Nop) { | ||
$namespace->stmts[] = new Nop(); | ||
} | ||
} | ||
|
||
private function findLastUseIndex(Namespace_ $namespace): ?int | ||
{ | ||
$lastUseIndex = null; | ||
|
||
foreach ($namespace->stmts as $i => $stmt) { | ||
if ($stmt instanceof Use_) { | ||
$lastUseIndex = $i; | ||
} | ||
} | ||
|
||
return $lastUseIndex; | ||
} | ||
} | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Support/Parser/Visitors/ArrayVisitors/BaseArrayVisitor.php
This file contains hidden or 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,32 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors; | ||
|
||
use PhpParser\NodeVisitorAbstract; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
|
||
abstract class BaseArrayVisitor extends NodeVisitorAbstract | ||
{ | ||
public function __construct( | ||
protected array $stmtNames, | ||
protected string $value, | ||
) | ||
{ | ||
} | ||
|
||
protected function isTargetProperty(string $propName, $default): bool | ||
{ | ||
return in_array($propName, $this->stmtNames, true) | ||
&& $default instanceof Array_; | ||
} | ||
|
||
protected function getTargetItems(array $items): array | ||
{ | ||
$filteredArray = array_filter($items, fn($item) => !$this->isTargetItem($item)); | ||
|
||
return array_values($filteredArray); | ||
} | ||
|
||
abstract protected function isTargetItem(?ArrayItem $item): bool; | ||
} |
26 changes: 26 additions & 0 deletions
26
.../Parser/Visitors/ArrayVisitors/MethodReturnArrayVisitors/BaseMethodReturnArrayVisitor.php
This file contains hidden or 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,26 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\MethodReturnArrayVisitors; | ||
|
||
use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\BaseArrayVisitor; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\Node\Expr\Array_; | ||
|
||
class BaseMethodReturnArrayVisitor extends BaseArrayVisitor | ||
{ | ||
protected function isTargetItem(?ArrayItem $item): bool | ||
{ | ||
return !empty($item) | ||
&& $item->key instanceof String_ | ||
&& $item->key->value === $this->value; | ||
} | ||
|
||
protected function isTargetReturnArray(Node $stmt): bool | ||
{ | ||
return $stmt instanceof Return_ | ||
&& $stmt->expr instanceof Array_; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...itors/ArrayVisitors/MethodReturnArrayVisitors/RemoveValueFromMethodReturnArrayVisitor.php
This file contains hidden or 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,28 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\MethodReturnArrayVisitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
|
||
class RemoveValueFromMethodReturnArrayVisitor extends BaseMethodReturnArrayVisitor | ||
{ | ||
public function enterNode(Node $node): void | ||
{ | ||
if (!($node instanceof ClassMethod)) { | ||
return; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
|
||
if (!in_array($methodName, $this->stmtNames, true)) { | ||
return; | ||
} | ||
|
||
foreach ($node->stmts as $stmt) { | ||
if ($this->isTargetReturnArray($stmt)) { | ||
$stmt->expr->items = $this->getTargetItems($stmt->expr->items); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...itors/ArrayVisitors/PropertyArrayVisitors/AddValueToArrayPropertyPropertyArrayVisitor.php
This file contains hidden or 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 RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
|
||
class AddValueToArrayPropertyPropertyArrayVisitor extends BasePropertyArrayVisitor | ||
{ | ||
public function enterNode(Node $node): void | ||
{ | ||
if (!$node instanceof Property) { | ||
return; | ||
} | ||
|
||
$property = $node->props[0]; | ||
|
||
$propertyName = $property->name->toString(); | ||
|
||
if (!$this->isTargetProperty($propertyName, $property->default)) { | ||
return; | ||
} | ||
|
||
$array = $property->default; | ||
|
||
if ($this->getTargetItems($array->items) !== $array->items) { | ||
return; | ||
} | ||
|
||
$array->items[] = new ArrayItem(new String_($this->value)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Support/Parser/Visitors/ArrayVisitors/PropertyArrayVisitors/BasePropertyArrayVisitor.php
This file contains hidden or 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,15 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors; | ||
|
||
use PhpParser\Node\Expr\ArrayItem; | ||
use RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\BaseArrayVisitor; | ||
|
||
class BasePropertyArrayVisitor extends BaseArrayVisitor | ||
{ | ||
protected function isTargetItem(?ArrayItem $item): bool | ||
{ | ||
return !empty($item) | ||
&& $item->value->value === $this->value; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../ArrayVisitors/PropertyArrayVisitors/RemoveValueFromArrayPropertyPropertyArrayVisitor.php
This file contains hidden or 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,28 @@ | ||
<?php | ||
|
||
namespace RonasIT\ProjectInitializator\Support\Parser\Visitors\ArrayVisitors\PropertyArrayVisitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Property; | ||
|
||
class RemoveValueFromArrayPropertyPropertyArrayVisitor extends BasePropertyArrayVisitor | ||
{ | ||
public function enterNode(Node $node): void | ||
{ | ||
if (!$node instanceof Property) { | ||
return; | ||
} | ||
|
||
$property = $node->props[0]; | ||
|
||
$propertyName = $property->name->toString(); | ||
|
||
if (!$this->isTargetProperty($propertyName, $property->default)) { | ||
return; | ||
} | ||
|
||
$array = $property->default; | ||
|
||
$array->items = $this->getTargetItems($array->items); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method assumes the item is not null but doesn't check for null before accessing $item->key. This could cause a null pointer exception if a null ArrayItem is passed.
Copilot uses AI. Check for mistakes.