Skip to content
Merged
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ $ phpactor generate:snippet implement_missing_methods tests/System/SystemTestCas

Will return, as a single string, the missing methods.

### Undeclared properties

Generates a snippet containing property definitions any assigns to
non-existing properties.

```bash
$ phpactor generate:snippet implement_missing_properties tests/System/SystemTestCase.php
```

Why?
----

Expand All @@ -67,6 +76,7 @@ Key bindings:

```
nnoremap <silent><leader>mm :call phpactor#generate_snippet("implement_missing_methods")<CR>
nnoremap <silent><leader>mm :call phpactor#generate_snippet("implement_missing_properties")<CR>
```

TODO
Expand Down
66 changes: 66 additions & 0 deletions lib/AstVisitor/AssignedPropertiesVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Phpactor\AstVisitor;

use PhpParser\NodeVisitor;
use PhpParser\Node\Expr;

class AssignedPropertiesVisitor implements NodeVisitor
{
private $assignedProperties = [];

/**
* {@inheritDoc}
*/
public function beforeTraverse(array $nodes)
{
}


/**
* {@inheritDoc}
*/
public function enterNode(\PhpParser\Node $node)
{
if (!$node instanceof Expr\Assign) {
return;
}

if (!$node->var instanceof Expr\PropertyFetch) {
return;
}

if (!isset($node->var->var)) {
return;
}

// we only care about direct assignations.
// e.g. $this->foo = 'bar' and not $this->bar->foo = 'bar'
if ($node->var->var->name !== 'this') {
return;
}

$this->assignedProperties[] = $node;
}


/**
* {@inheritDoc}
*/
public function leaveNode(\PhpParser\Node $node)
{
}


/**
* {@inheritDoc}
*/
public function afterTraverse(array $nodes)
{
}

public function getAssignedPropertyNodes()
{
return $this->assignedProperties;
}
}
15 changes: 14 additions & 1 deletion lib/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
use BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Phpactor\Console\Command\GenerateSnippetCommand;
use Phpactor\Util\ClassUtil;
use Phpactor\Generation\Snippet\ImplementMissingMethodsGenerator;
use Phpactor\Generation\Snippet\MissingMethodsGenerator;
use Phpactor\Generation\SnippetGeneratorRegistry;
use Phpactor\Generation\Snippet\ImplementMissingMethodsGenerator;
use Phpactor\Generation\Snippet\MissingPropertiesGenerator;

class CoreExtension implements ExtensionInterface
{
Expand Down Expand Up @@ -162,6 +164,10 @@ private function registerGeneration(Container $container)
'implement_missing_methods',
$container->get('generator.snippet.implement_missing_methods')
);
$registry->register(
'implement_missing_properties',
$container->get('generator.snippet.implement_missing_properties')
);

return $registry;
});
Expand All @@ -172,5 +178,12 @@ private function registerGeneration(Container $container)
$container->get('util.class')
);
});

$container->register('generator.snippet.implement_missing_properties', function(Container $container) {
return new MissingPropertiesGenerator(
$container->get('reflector'),
$container->get('util.class')
);
});
}
}
115 changes: 115 additions & 0 deletions lib/Generation/Snippet/MissingPropertiesGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Phpactor\Generation\Snippet;

use BetterReflection\Reflector\ClassReflector;
use Phpactor\CodeContext;
use Phpactor\Util\ClassUtil;
use BetterReflection\Reflection\ReflectionMethod;
use Phpactor\Generation\SnippetGeneratorInterface;
use PhpParser\NodeTraverser;
use Phpactor\AstVisitor\AssignedPropertiesVisitor;
use PhpParser\Node;
use BetterReflection\Util\Visitor\VariableCollectionVisitor;
use BetterReflection\NodeCompiler\CompilerContext;
use BetterReflection\Reflection\ReflectionClass;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Scalar\DNumber;

class MissingPropertiesGenerator implements SnippetGeneratorInterface
{
/**
* @var ClassReflector
*/
private $classReflector;

/**
* @var ClassUtil
*/
private $classUtil;

/**
* @var AssignedPropertiesVisitor
*/
private $assignedPropertiesVisitor;

public function __construct(
ClassReflector $classReflector,
ClassUtil $classUtil,
AssignedPropertiesVisitor $assignedPropertiesVisitor = null
)
{
$this->classReflector = $classReflector;
$this->classUtil = $classUtil;
$this->assignedPropertiesVisitor = $assignedPropertiesVisitor ?: new AssignedPropertiesVisitor();
}

public function generate(CodeContext $codeContext): string
{
$reflection = $this->classReflector->reflect(
$this->classUtil->getClassNameFromSource($codeContext->getSource())
);

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($propertyVisitor = $this->assignedPropertiesVisitor);
$nodeTraverser->addVisitor($variableVisitor = new VariableCollectionVisitor(new CompilerContext($this->classReflector, $reflection)));
$nodeTraverser->traverse([$reflection->getAst()]);

$missingProperties = $this->resolveMissingProperties($propertyVisitor, $reflection);

$snippet = [];

foreach ($missingProperties as $missingProperty) {
$type = $this->resolveType($variableVisitor, $missingProperty);
$snippet[] = '/**';
$snippet[] = ' * @var ' . ($type ?: 'mixed');
$snippet[] = ' */';
$snippet[] = sprintf(
'private $%s;' . PHP_EOL,
$missingProperty->var->name
);
}

return implode(PHP_EOL, $snippet);
}

public function resolveMissingProperties(AssignedPropertiesVisitor $visitor, ReflectionClass $reflection)
{

$assigned = $visitor->getAssignedPropertyNodes();
$properties = $reflection->getProperties();

return array_filter($assigned, function (Node $property) use ($properties) {
return !array_key_exists($property->var->name, $properties);
});
}

private function resolveType(VariableCollectionVisitor $visitor, Node $propertyAssign)
{
$variables = $visitor->getVariables();
$assignedVar = $propertyAssign->expr;

if ($assignedVar instanceof String_ || $assignedVar instanceof EncapsedStringPart) {
return 'string';
} elseif ($assignedVar instanceof LNumber) {
return 'int';
} elseif ($assignedVar instanceof DNumber) {
return 'float';
} elseif ($assignedVar instanceof New_) {
return (string) $assignedVar->class;
}

// TODO: Take into account variable scope -- this just does a name
// match on all variables in any method and takes the first one.
foreach ($variables as $variable) {
if ($variable->getName() != $assignedVar->name) {
continue;
}

return (string) $variable->getType();
}
}
}
23 changes: 23 additions & 0 deletions lib/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Phpactor;

use Symfony\Component\Console\Input\InputInterface;

class Test implements InputInterface
{

public function __construct(\stdClass $foo)
{
$this->foo = 'foo';
$this->baz = $foo;
}

public function foobar(Foobar $foobar, BarFoo $barfoo, $arse = 'string')
{
$this->foobar = $foobar;
$this->barfoo = $barfoo;
$this->arse = $arse;
$this->boo = 'boo';
}
}
18 changes: 0 additions & 18 deletions tests/Functional/Generation/classesasd/AbstractExample.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/Functional/Generation/classesasd/Example1Interface.php

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions tests/System/Command/GenerateSnippetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function provideGenerate()
return [
[
'implement_missing_methods',
'implement_missing_properties',
],
];
}
Expand Down
Loading