-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from permafrost-dev/add-class-def-support
Add class definition support
- Loading branch information
Showing
39 changed files
with
545 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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,25 @@ | ||
<?php | ||
|
||
namespace Permafrost\PhpCodeSearch\Results\Nodes; | ||
|
||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\BootsTraits; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasName; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasValue; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasVisibility; | ||
use PhpParser\Node; | ||
|
||
class ClassConstantNode implements ResultNode | ||
{ | ||
use BootsTraits; | ||
use HasLocation; | ||
use HasName; | ||
use HasValue; | ||
use HasVisibility; | ||
|
||
public function __construct(Node\Stmt\ClassConst $node) | ||
{ | ||
$this->bootTraits($node); | ||
$this->bootHasValue($node->consts[0]); | ||
} | ||
} |
This file contains 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,41 @@ | ||
<?php | ||
|
||
namespace Permafrost\PhpCodeSearch\Results\Nodes; | ||
|
||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\BootsTraits; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasName; | ||
use Permafrost\PhpCodeSearch\Support\NameResolver; | ||
use Permafrost\PhpCodeSearch\Support\StatementTransformer; | ||
use PhpParser\Node; | ||
|
||
class ClassDefinitionNode implements ResultNode | ||
{ | ||
use BootsTraits; | ||
use HasName; | ||
use HasLocation; | ||
|
||
/** @var array|ResultNode[]|ValueNode[] */ | ||
public $properties; | ||
|
||
/** @var array|ResultNode[]|ValueNode[] */ | ||
public $methods; | ||
|
||
public $implements = []; | ||
|
||
public $extends; | ||
|
||
/** @var array|ResultNode[]|ValueNode[] */ | ||
public $constants = []; | ||
|
||
public function __construct(Node\Stmt\Class_ $node) | ||
{ | ||
$this->bootTraits($node); | ||
|
||
$this->extends = NameResolver::resolve($node->extends); | ||
$this->implements = NameResolver::resolveAll($node->implements); | ||
$this->properties = StatementTransformer::parserNodesToResultNode($node->getProperties()); | ||
$this->methods = StatementTransformer::parserNodesToResultNode($node->getMethods()); | ||
$this->constants = StatementTransformer::parserNodesToResultNode($node->getConstants()); | ||
} | ||
} |
This file contains 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,40 @@ | ||
<?php | ||
|
||
namespace Permafrost\PhpCodeSearch\Results\Nodes; | ||
|
||
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\BootsTraits; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasName; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasVisibility; | ||
use Permafrost\PhpCodeSearch\Support\NameResolver; | ||
use Permafrost\PhpCodeSearch\Support\StatementTransformer; | ||
use PhpParser\Node; | ||
|
||
class ClassMethodNode implements ResultNode | ||
{ | ||
use BootsTraits; | ||
use HasName; | ||
use HasLocation; | ||
use HasVisibility; | ||
|
||
/** @var string|null */ | ||
public $returnType; | ||
|
||
public $isStatic = false; | ||
|
||
public $isAbstract = false; | ||
|
||
/** @var array|ResultNode[]|ValueNode[] */ | ||
public $params = []; | ||
|
||
public function __construct(Node\Stmt\ClassMethod $node) | ||
{ | ||
$this->bootTraits($node); | ||
|
||
$this->isStatic = $node->isStatic(); | ||
$this->isAbstract = $node->isAbstract(); | ||
$this->returnType = NameResolver::resolve($node->getReturnType()); | ||
$this->params = StatementTransformer::parserNodesToResultNode($node->params); | ||
} | ||
} |
This file contains 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 Permafrost\PhpCodeSearch\Results\Nodes; | ||
|
||
use Permafrost\PhpCodeSearch\Code\GenericCodeLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\BootsTraits; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasLocation; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasName; | ||
use Permafrost\PhpCodeSearch\Results\Nodes\Traits\HasVisibility; | ||
use Permafrost\PhpCodeSearch\Support\ExpressionTransformer; | ||
use PhpParser\Node; | ||
|
||
class ClassPropertyNode implements ResultNode | ||
{ | ||
use BootsTraits; | ||
use HasName; | ||
use HasLocation; | ||
use HasVisibility; | ||
|
||
/** @var ResultNode|ValueNode|null */ | ||
public $default; | ||
|
||
public $isStatic = false; | ||
|
||
public function __construct(Node\Stmt\Property $node) | ||
{ | ||
$this->bootTraits($node); | ||
|
||
$this->isStatic = $node->isStatic(); | ||
$this->default = ExpressionTransformer::parserNodeToResultNode($node->props[0]->default); | ||
} | ||
} |
This file contains 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 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 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,29 @@ | ||
<?php | ||
|
||
namespace Permafrost\PhpCodeSearch\Results\Nodes\Traits; | ||
|
||
use Permafrost\PhpCodeSearch\Support\Arr; | ||
|
||
trait BootsTraits | ||
{ | ||
protected function bootTraits($node): void | ||
{ | ||
$reflectionObject = new \ReflectionObject($this); | ||
|
||
collect($reflectionObject->getTraitNames()) | ||
->map(function(string $name){ | ||
if (strpos($name, __NAMESPACE__) === false) { | ||
return null; | ||
} | ||
return Arr::last(explode('\\', $name)); | ||
}) | ||
->filter() | ||
->each(function(string $name) use ($node) { | ||
$bootMethodName = 'boot'.$name; | ||
|
||
if (method_exists($this, $bootMethodName)) { | ||
$this->$bootMethodName($node); | ||
} | ||
}); | ||
} | ||
} |
This file contains 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 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 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,24 @@ | ||
<?php | ||
|
||
namespace Permafrost\PhpCodeSearch\Results\Nodes\Traits; | ||
|
||
use Permafrost\PhpCodeSearch\Results\Nodes\Scalar\StringNode; | ||
use Permafrost\PhpCodeSearch\Support\ExpressionTransformer; | ||
|
||
trait HasValue | ||
{ | ||
/** @var string|StringNode */ | ||
public $value; | ||
|
||
public function value() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
protected function bootHasValue($node): void | ||
{ | ||
if (property_exists($node, 'value')) { | ||
$this->value = ExpressionTransformer::parserNodeToResultNode($node->value); | ||
} | ||
} | ||
} |
This file contains 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 Permafrost\PhpCodeSearch\Results\Nodes\Traits; | ||
|
||
trait HasVisibility { | ||
|
||
/** @var string */ | ||
public $visibility = 'unknown'; | ||
|
||
protected function bootHasVisibility($node): void | ||
{ | ||
$this->initVisibilityAttribute($node); | ||
} | ||
|
||
protected function initVisibilityAttribute($node): void | ||
{ | ||
$visibilityMap = [ | ||
'isPublic' => 'public', | ||
'isPrivate' => 'private', | ||
'isProtected' => 'protected', | ||
]; | ||
|
||
foreach($visibilityMap as $method => $visibility) { | ||
if ($node->$method()) { | ||
$this->visibility = $visibility; | ||
return; | ||
} | ||
} | ||
|
||
$this->visibility = 'unknown'; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.