Skip to content

Commit

Permalink
feat: cast node to dto
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuftaufiq committed Apr 18, 2022
1 parent 82188d4 commit c4c086b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/Casts/AbstractNodeCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Casts;

abstract class AbstractNodeCast
{
/**
* Undocumented variable
*
* @var array<string, array<string, string>>
*/
protected $map = [
'cores' => [
'benchmark' => 'CI_Benchmark',
'config' => 'CI_Config',
'input' => 'CI_Input',
'lang' => 'CI_Lang',
'load' => 'CI_Loader',
'output' => 'CI_Output',
'security' => 'CI_Security',
'uri' => 'CI_URI',
],
'libraries' => [
'cache' => 'CI_Cache',
'calendar' => 'CI_Calendar',
// 'driver' => 'CI_Driver',
'email' => 'CI_Email',
'encryption' => 'CI_Encryption',
'form_validation' => 'CI_Form_validation',
'ftp' => 'CI_Ftp',
'image_lib' => 'CI_Image_lib',
'migration' => 'CI_Migration',
'pagination' => 'CI_Pagination',
'parser' => 'CI_Parser',
'profiler' => 'CI_Profiler',
'session' => 'CI_Session',
'table' => 'CI_Table',
'trackback' => 'CI_Trackback',
'typography' => 'CI_Typography',
'unit_test' => 'CI_Unit_test',
'upload' => 'CI_Upload',
'user_agent' => 'CI_User_agent',
'xmlrpc' => 'CI_Xmlrpc',
'xmlrpcs' => 'CI_Xmlrpcs',
'zip' => 'CI_Zip',
],
];
}
70 changes: 70 additions & 0 deletions src/Casts/NodeLibraryCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Casts;

use Haemanthus\CodeIgniter3IdeHelper\Objects\DocumentBlockDTO;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;

class NodeLibraryCast extends AbstractNodeCast
{
protected const KEY = 'libraries';

protected function classType(string $name): string
{
if (array_key_exists($name, $this->map[self::KEY]) === true) {
return $this->map[self::KEY][$name];
}

return $name;
}

/**
* Undocumented function
*
* @param array<Arg> $args
* @return bool
*/
protected function isArgsTypeScalarString(array $args): bool
{
return array_reduce($args, fn (bool $carry, Arg $arg): bool => (
$arg->name === null
&& ($arg->value instanceof String_ || $arg->value instanceof ConstFetch)
&& $carry
), true);
}

/**
* Undocumented function
*
* @param array<Arg> $args
* @return DocumentBlockDTO
*/
protected function castScalarStringArg(array $args): DocumentBlockDTO
{
$name = array_key_exists(2, $args) ? $args[2]->value->value : $args[0]->value->value;
$type = $this->classType($args[0]->value->value);

return new DocumentBlockDTO(
$name,
$type,
);
}

public function cast(MethodCall $node): ?DocumentBlockDTO
{
switch (true) {
case $this->isArgsTypeScalarString($node->args):
$block = $this->castScalarStringArg($node->args);
break;

default:
$block = null;
break;
}

return $block;
}
}
28 changes: 28 additions & 0 deletions src/Objects/DocumentBlockDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Haemanthus\CodeIgniter3IdeHelper\Objects;

class DocumentBlockDTO
{
protected string $name;

protected ?string $type;

public function __construct(
string $name,
?string $type = null
) {
$this->name = $name;
$this->type = $type;
}

public function getName(): string
{
return $this->name;
}

public function getType(): ?string
{
return $this->type;
}
}
16 changes: 13 additions & 3 deletions src/Parsers/CoreFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@

namespace Haemanthus\CodeIgniter3IdeHelper\Parsers;

use Haemanthus\CodeIgniter3IdeHelper\Casts\NodeLibraryCast;
use Haemanthus\CodeIgniter3IdeHelper\Objects\DocumentBlockDTO;
use Haemanthus\CodeIgniter3IdeHelper\Visitors\MethodCallNodeVisitor;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;

class CoreFileParser extends AbstractFileParser
{
protected MethodCallNodeVisitor $visitor;

protected NodeLibraryCast $nodeLibraryCast;

public function __construct(
ParserFactory $parser,
NodeTraverser $traverser
) {
parent::__construct($parser, $traverser);
$this->visitor = new MethodCallNodeVisitor();
$this->traverser->addVisitor($this->visitor);
$this->nodeLibraryCast = new NodeLibraryCast();
}

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

return array_merge(
$libraries = array_map(
fn (MethodCall $library): ?DocumentBlockDTO => $this->nodeLibraryCast->cast($library),
$this->visitor->getFoundLoadLibraryNodes(),
$this->visitor->getFoundLoadModelNodes(),
);

$models = $this->visitor->getFoundLoadModelNodes();

return array_merge($libraries, $models);
}
}

0 comments on commit c4c086b

Please sign in to comment.