Skip to content
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

AST test with EnhancedVariableExtractor #241

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cakephp/cakephp": "^4.0.2",
"cakephp/bake": "^2.1.0",
"sebastian/diff": "^3.0.1 || ^4.0.1",
"nikic/php-parser": "^4.12",
"squizlabs/php_codesniffer": "^3.5.2"
},
"require-dev": {
Expand Down
89 changes: 89 additions & 0 deletions src/Annotator/Template/EnhancedVariableExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace IdeHelper\Annotator\Template;

use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\ParserFactory;
use Throwable;

/**
* Extracts variables from CakePHP php/ctp templates using AST.
*/
class EnhancedVariableExtractor {

/**
* @param string $content
* @return array
*/
public function extract(string $content) {
$nodes = $this->parse($content);

$result = [];

$nodeFinder = new NodeFinder();

//TODO: this finds too many!

/** @var \PhpParser\Node\Expr\Variable[] $variables */
$variables = $nodeFinder->findInstanceOf($nodes, Node\Expr\Variable::class);

foreach ($variables as $variable) {
if ($variable->name === 'this') {
continue;
}

$result[$variable->name] = [
'name' => $variable->name,
];
}

return $result;
}

/**
* @param string $content
*
* @return Node[]
*/
protected function parse(string $content): array {
$parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7);
try {
$ast = $parser->parse($content);

// throwing error handler
assert($ast !== null);

$ast = $this->resolveNames($ast);

} catch (Throwable $exception) {
return [];
}

return $ast;
}

/**
* Resolves namespaced names in the AST.
*
* @param Node[] $ast
*
* @return Node[]
*/
protected function resolveNames(array $ast) : array
{
$nodeTraverser = new NodeTraverser();

$nodeTraverser->addVisitor(new NameResolver());
$nodeTraverser->addVisitor(new NodeConnectingVisitor());

//$variableFinder= new VariableFinder();
//$nodeTraverser->addVisitor($variableFinder);

return $nodeTraverser->traverse($ast);
}

}
9 changes: 9 additions & 0 deletions src/Annotator/Template/Visitor/VariableFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace IdeHelper\Annotator\Template\Visitor;

use PhpParser\NodeVisitor\FindingVisitor;

class VariableFinder extends FindingVisitor
{
}
5 changes: 5 additions & 0 deletions src/Annotator/TemplateAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cake\View\View;
use IdeHelper\Annotation\AnnotationFactory;
use IdeHelper\Annotation\VariableAnnotation;
use IdeHelper\Annotator\Template\EnhancedVariableExtractor;
use IdeHelper\Annotator\Template\VariableExtractor;
use IdeHelper\Utility\App;
use PHP_CodeSniffer\Files\File;
Expand Down Expand Up @@ -445,6 +446,10 @@ protected function buildAnnotations(string $path, string $content): array {
protected function getTemplateVariables($path, $content) {
$file = $this->getFile($path, $content);

$extractor = new EnhancedVariableExtractor();
$x = $extractor->extract($content);
dd($x);

$class = Configure::read('IdeHelper.variableExtractor') ?: VariableExtractor::class;
/** @var \IdeHelper\Annotator\Template\VariableExtractor $extractor */
$extractor = new $class();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace IdeHelper\Test\TestCase\Annotator\Template;

use IdeHelper\Annotator\Template\EnhancedVariableExtractor;
use IdeHelper\Annotator\Traits\FileTrait;
use Shim\TestSuite\TestCase;

/**
* @property \IdeHelper\Annotator\Template\EnhancedVariableExtractor $variableExtractor
*/
class EnhancedVariableExtractorTest extends TestCase {

use FileTrait;

/**
* @return void
*/
public function setUp(): void {
parent::setUp();

$this->variableExtractor = new EnhancedVariableExtractor();
}

/**
* @return void
*/
public function testExtract() {
$path = TEST_ROOT . 'templates' . DS . 'Foos' . DS . 'vars.php';
$content = file_get_contents($path);

$result = $this->variableExtractor->extract($content);

//TODO/FIXME
$expected = [
'obj' => [
'name' => 'obj'
],
'allCars' => [
'name' => 'allCars'
],
'car' => [
'name' => 'car'
],
'finalCarTime' => [
'name' => 'finalCarTime'
],
'wheel' => [
'name' => 'wheel'
],
'date' => [
'name' => 'date'
],
'i' => [
'name' => 'i'
],
'engine' => [
'name' => 'engine'
],
'foos' => [
'name' => 'foos'
],
'foo' => [
'name' => 'foo'
]
];
debug($result);
$this->assertEquals($expected, $result);
}
}