Skip to content

Commit

Permalink
Refactor reflection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin v / vvval committed Apr 15, 2019
1 parent 84de634 commit 78afc81
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 27 deletions.
Expand Up @@ -5,7 +5,7 @@

use Cycle\ORM\Promise\Declaration\DeclarationInterface;

final class ReflectionDeclarationDeclaration implements DeclarationInterface
final class ReflectionDeclaration implements DeclarationInterface
{
/** @var \ReflectionClass */
private $reflection;
Expand All @@ -27,6 +27,6 @@ public function getNamespaceName(): ?string

public function getFullName(): string
{
return $this->reflection->getName();
return $this->reflection->name;
}
}
2 changes: 1 addition & 1 deletion src/Declaration/Declarations.php
Expand Up @@ -14,7 +14,7 @@ class Declarations
public static function createFromReflection(\ReflectionClass $parent, string $class): self
{
$self = new self();
$self->parent = new Declaration\ReflectionDeclarationDeclaration($parent);
$self->parent = new Declaration\ReflectionDeclaration($parent);
$self->class = new Declaration\DeclarationDeclaration($class, $self->parent);

return $self;
Expand Down
6 changes: 3 additions & 3 deletions src/Declaration/Extractor/Methods.php
Expand Up @@ -35,7 +35,7 @@ public function getMethods(\ReflectionClass $reflection): array
continue;
}

$methods[] = new Node\Stmt\ClassMethod($method->getName(), [
$methods[] = new Node\Stmt\ClassMethod($method->name, [
'flags' => $this->packFlags($method),
'returnType' => $this->defineReturnType($method),
'params' => $this->packParams($method),
Expand All @@ -48,7 +48,7 @@ public function getMethods(\ReflectionClass $reflection): array

private function isIgnoredMethod(\ReflectionMethod $method): bool
{
return $method->isPrivate() || $method->isStatic() || $method->isFinal() || $method->isAbstract() || $this->isMagicMethod($method->getName());
return $method->isPrivate() || $method->isStatic() || $method->isFinal() || $method->isAbstract() || $this->isMagicMethod($method->name);
}

private function isMagicMethod(string $name): bool
Expand Down Expand Up @@ -99,7 +99,7 @@ private function packParams(\ReflectionMethod $method): array
{
$params = [];
foreach ($method->getParameters() as $parameter) {
$param = new Param($parameter->getName());
$param = new Param($parameter->name);

$type = $this->defineParamReturnType($parameter);
if ($type !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Declaration/Extractor/Properties.php
Expand Up @@ -14,7 +14,7 @@ public function getProperties(\ReflectionClass $reflection): array
continue;
}

$properties[] = $property->getName();
$properties[] = $property->name;
}

return $properties;
Expand Down
9 changes: 2 additions & 7 deletions src/Materizalizer/EvalMaterializer.php
Expand Up @@ -3,6 +3,7 @@
namespace Cycle\ORM\Promise\Materizalizer;

use Cycle\ORM\Promise\MaterializerInterface;
use Cycle\ORM\Promise\Utils;

final class EvalMaterializer implements MaterializerInterface
{
Expand All @@ -12,12 +13,6 @@ final class EvalMaterializer implements MaterializerInterface
*/
public function materialize(string $code, string $shortClassName, \ReflectionClass $reflection): void
{
if (mb_strpos($code, '<?php') === 0) {
$code = mb_substr($code, 5);
} elseif (mb_strpos($code, '<?') === 0) {
$code = mb_substr($code, 2);
}

eval($code);
eval(Utils::trimPHPOpenTag($code));
}
}
9 changes: 2 additions & 7 deletions src/Materizalizer/FileMaterializer.php
Expand Up @@ -4,6 +4,7 @@
namespace Cycle\ORM\Promise\Materizalizer;

use Cycle\ORM\Promise\MaterializerInterface;
use Cycle\ORM\Promise\Utils;
use Spiral\Core\Container\SingletonInterface;

class FileMaterializer implements MaterializerInterface, SingletonInterface
Expand Down Expand Up @@ -46,13 +47,7 @@ private function makeFilename(string $className): string

private function prepareCode(string $code): string
{
if (mb_strpos($code, '<?php') === 0) {
$code = mb_substr($code, 5);
} elseif (mb_strpos($code, '<?') === 0) {
$code = mb_substr($code, 2);
}

return "<?php\n" . trim($code);
return "<?php\n" . trim(Utils::trimPHPOpenTag($code));
}

private function create(string $filename, string $code): void
Expand Down
2 changes: 1 addition & 1 deletion src/Names.php
Expand Up @@ -7,7 +7,7 @@ class Names
{
public function make(\ReflectionClass $reflection, ?string $namespace = null): string
{
$hash = hash('sha256', $reflection->getName() . $reflection->getFileName());
$hash = hash('sha256', $reflection->name . $reflection->getFileName());

$name = "{$reflection->getShortName()}Proxy_$hash";
if ($namespace !== null) {
Expand Down
6 changes: 3 additions & 3 deletions src/ProxyPrinter.php
Expand Up @@ -114,10 +114,10 @@ private function propertyType(): string
}

/**
* @return Node\Stmt[]|null
* @return Node\Stmt[]
*/
private function getNodesFromStub(): ?array
private function getNodesFromStub(): array
{
return $this->parser->parse($this->stubs->getContent());
return $this->parser->parse($this->stubs->getContent()) ?? [];
}
}
20 changes: 20 additions & 0 deletions src/Utils.php
Expand Up @@ -56,4 +56,24 @@ public static function trimTrailingDigits(string $name, int $number): string

return mb_substr($name, 0, $pos);
}

/**
* Remove any kinds of php open tags.
*
* @param string $code
*
* @return string
*/
public static function trimPHPOpenTag(string $code): string
{
if (mb_strpos($code, '<?php') === 0) {
return mb_substr($code, 5);
}

if (mb_strpos($code, '<?') === 0) {
return mb_substr($code, 2);
}

return $code;
}
}
4 changes: 2 additions & 2 deletions src/Visitor/AddUseStmts.php
Expand Up @@ -46,8 +46,8 @@ private function definePlacementID(Node\Stmt\Namespace_ $node): int
}

/**
* @param Node\Stmt[] $stmts
* @param Node\Stmt\Use_[] $nodes
* @param Node\Stmt[] $stmts
* @param Node[] $nodes
*
* @return Node\Stmt\Use_[]
*/
Expand Down

0 comments on commit 78afc81

Please sign in to comment.