Skip to content

Commit

Permalink
feat: wrap properties inside class dto
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed Apr 21, 2022
1 parent 8068a02 commit 1f750f0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
30 changes: 30 additions & 0 deletions src/Objects/ClassDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Objects;

use PhpParser\Node\Stmt\Class_;

class ClassDto
{
protected Class_ $node;

protected array $properties;

public function __construct(
Class_ $node,
array $properties = []
) {
$this->node = $node;
$this->properties = $properties;
}

public function getNode(): Class_
{
return $this->node;
}

public function getProperties(): array
{
return $this->properties;
}
}
22 changes: 13 additions & 9 deletions src/Parsers/CoreFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Haemanthus\CodeIgniter3IdeHelper\Casts\LoadLibraryNodeCast;
use Haemanthus\CodeIgniter3IdeHelper\Casts\LoadModelNodeCast;
use Haemanthus\CodeIgniter3IdeHelper\Objects\ClassDto;
use Haemanthus\CodeIgniter3IdeHelper\Visitors\ClassNodeVisitor;
use Haemanthus\CodeIgniter3IdeHelper\Visitors\MethodCallNodeVisitor;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -12,9 +13,9 @@

class CoreFileParser extends AbstractFileParser
{
protected ClassNodeVisitor $classVisitor;
protected ClassNodeVisitor $classNodeVisitor;

protected MethodCallNodeVisitor $methodCallVisitor;
protected MethodCallNodeVisitor $methodCallNodeVisitor;

protected LoadLibraryNodeCast $loadLibraryNodeCast;

Expand All @@ -30,27 +31,30 @@ public function __construct(
$this->loadLibraryNodeCast = $loadLibraryNodeCast;
$this->loadModelNodeCast = $loadModelNodeCast;

$this->classVisitor = new ClassNodeVisitor();
$this->methodCallVisitor = new MethodCallNodeVisitor();
$this->classNodeVisitor = new ClassNodeVisitor();
$this->methodCallNodeVisitor = new MethodCallNodeVisitor();

$this->traverser->addVisitor($this->classVisitor);
$this->traverser->addVisitor($this->methodCallVisitor);
$this->traverser->addVisitor($this->classNodeVisitor);
$this->traverser->addVisitor($this->methodCallNodeVisitor);
}

public function parse(string $contents): array
{
$this->traverser->traverse($this->parser->parse($contents));

$loadLibraryNodes = $this->methodCallVisitor->getFoundLoadLibraryNodes();
$loadLibraryNodes = $this->methodCallNodeVisitor->getFoundLoadLibraryNodes();
$loadLibraryTags = array_reduce($loadLibraryNodes, fn (array $carry, MethodCall $node): array => (
array_merge($carry, $this->loadLibraryNodeCast->cast($node))
), []);

$loadModelNodes = $this->methodCallVisitor->getFoundLoadModelNodes();
$loadModelNodes = $this->methodCallNodeVisitor->getFoundLoadModelNodes();
$loadModelTags = array_reduce($loadModelNodes, fn (array $carry, MethodCall $node): array => (
array_merge($carry, $this->loadModelNodeCast->cast($node))
), []);

return array_merge($loadLibraryTags, $loadModelTags);
return [new ClassDto(
$this->classNodeVisitor->getFoundClassNode(),
array_merge($loadLibraryTags, $loadModelTags)
)];
}
}

0 comments on commit 1f750f0

Please sign in to comment.