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

Updating Tokenizer code with PHP 8.1 features #647

Merged
merged 1 commit into from
Apr 3, 2022
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
28 changes: 9 additions & 19 deletions src/Tokenizer/src/AbstractLocator.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer;
Expand All @@ -27,12 +20,9 @@ abstract class AbstractLocator implements InjectableInterface, LoggerAwareInterf

public const INJECTOR = Tokenizer::class;

/** @var Finder */
protected $finder;

public function __construct(Finder $finder)
{
$this->finder = $finder;
public function __construct(
protected Finder $finder
) {
}

/**
Expand All @@ -50,8 +40,8 @@ protected function availableReflections(): \Generator
if ($reflection->hasIncludes()) {
// We are not analyzing files which has includes, it's not safe to require such reflections
$this->getLogger()->warning(
sprintf('File `%s` has includes and excluded from analysis', $file),
compact('file')
\sprintf('File `%s` has includes and excluded from analysis', $file),
['file' => $file]
);

continue;
Expand All @@ -78,11 +68,11 @@ protected function classReflection(string $class): \ReflectionClass
return;
}

throw new LocatorException("Class '{$class}' can not be loaded");
throw new LocatorException(\sprintf("Class '%s' can not be loaded", $class));
};

//To suspend class dependency exception
spl_autoload_register($loader);
\spl_autoload_register($loader);

try {
//In some cases reflection can thrown an exception if class invalid or can not be loaded,
Expand All @@ -94,7 +84,7 @@ protected function classReflection(string $class): \ReflectionClass
}

$this->getLogger()->error(
sprintf(
\sprintf(
'%s: %s in %s:%s',
$class,
$e->getMessage(),
Expand All @@ -106,7 +96,7 @@ protected function classReflection(string $class): \ReflectionClass

throw new LocatorException($e->getMessage(), $e->getCode(), $e);
} finally {
spl_autoload_unregister($loader);
\spl_autoload_unregister($loader);
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/Tokenizer/src/Bootloader/TokenizerBootloader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer\Bootloader;
Expand All @@ -19,6 +12,7 @@
use Spiral\Core\Container\SingletonInterface;
use Spiral\Tokenizer\ClassesInterface;
use Spiral\Tokenizer\ClassLocator;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\InvocationLocator;
use Spiral\Tokenizer\InvocationsInterface;
use Spiral\Tokenizer\ScopedClassesInterface;
Expand All @@ -33,12 +27,9 @@ final class TokenizerBootloader extends Bootloader implements SingletonInterface
InvocationsInterface::class => InvocationLocator::class,
];

/** @var ConfiguratorInterface */
private $config;

public function __construct(ConfiguratorInterface $config)
{
$this->config = $config;
public function __construct(
private readonly ConfiguratorInterface $config
) {
}

public function boot(Container $container, DirectoriesInterface $dirs): void
Expand All @@ -47,7 +38,7 @@ public function boot(Container $container, DirectoriesInterface $dirs): void
$container->bindInjector(InvocationLocator::class, Tokenizer::class);

$this->config->setDefaults(
'tokenizer',
TokenizerConfig::CONFIG,
[
'directories' => [$dirs->get('app')],
'exclude' => [
Expand All @@ -65,6 +56,6 @@ public function boot(Container $container, DirectoriesInterface $dirs): void
*/
public function addDirectory(string $directory): void
{
$this->config->modify('tokenizer', new Append('directories', null, $directory));
$this->config->modify(TokenizerConfig::CONFIG, new Append('directories', null, $directory));
}
}
22 changes: 6 additions & 16 deletions src/Tokenizer/src/ClassLocator.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer;
Expand All @@ -18,20 +11,17 @@
*/
final class ClassLocator extends AbstractLocator implements ClassesInterface
{
/**
* {@inheritdoc}
*/
public function getClasses($target = null): array
public function getClasses(object|string|null $target = null): array
{
if (!empty($target) && (is_object($target) || is_string($target))) {
if (!empty($target) && (\is_object($target) || \is_string($target))) {
$target = new \ReflectionClass($target);
}

$result = [];
foreach ($this->availableClasses() as $class) {
try {
$reflection = $this->classReflection($class);
} catch (LocatorException $e) {
} catch (LocatorException) {
//Ignoring
continue;
}
Expand All @@ -54,7 +44,7 @@ protected function availableClasses(): array
$classes = [];

foreach ($this->availableReflections() as $reflection) {
$classes = array_merge($classes, $reflection->getClasses());
$classes = \array_merge($classes, $reflection->getClasses());
}

return $classes;
Expand All @@ -73,10 +63,10 @@ protected function isTargeted(\ReflectionClass $class, \ReflectionClass $target

if (!$target->isTrait()) {
//Target is interface or class
return $class->isSubclassOf($target) || $class->getName() == $target->getName();
return $class->isSubclassOf($target) || $class->getName() === $target->getName();
}

//Checking using traits
return in_array($target->getName(), $this->fetchTraits($class->getName()));
return \in_array($target->getName(), $this->fetchTraits($class->getName()));
}
}
9 changes: 1 addition & 8 deletions src/Tokenizer/src/ClassesInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer;
Expand All @@ -26,5 +19,5 @@ interface ClassesInterface
* results.
* @return \ReflectionClass[]
*/
public function getClasses($target = null): array;
public function getClasses(object|string|null $target = null): array;
}
9 changes: 1 addition & 8 deletions src/Tokenizer/src/Config/TokenizerConfig.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer\Config;
Expand All @@ -29,7 +22,7 @@ final class TokenizerConfig extends InjectableConfig

public function getDirectories(): array
{
return $this->config['directories'] ?? [getcwd()];
return $this->config['directories'] ?? [\getcwd()];
}

public function getExcludes(): array
Expand Down
7 changes: 0 additions & 7 deletions src/Tokenizer/src/Exception/LocatorException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Tokenizer/src/Exception/ReflectionException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Tokenizer/src/Exception/TokenizerException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer\Exception;
Expand Down
18 changes: 4 additions & 14 deletions src/Tokenizer/src/InvocationLocator.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer;
Expand All @@ -22,9 +15,6 @@
*/
final class InvocationLocator extends AbstractLocator implements InvocationsInterface
{
/**
* {@inheritdoc}
*/
public function getInvocations(\ReflectionFunctionAbstract $function): array
{
$result = [];
Expand All @@ -45,12 +35,12 @@ public function getInvocations(\ReflectionFunctionAbstract $function): array
*/
protected function availableInvocations(string $signature = ''): \Generator
{
$signature = strtolower(trim($signature, '\\'));
$signature = \strtolower(\trim($signature, '\\'));
foreach ($this->availableReflections() as $reflection) {
foreach ($reflection->getInvocations() as $invocation) {
if (
!empty($signature)
&& strtolower(trim($invocation->getName(), '\\')) != $signature
&& \strtolower(\trim($invocation->getName(), '\\')) !== $signature
) {
continue;
}
Expand All @@ -68,7 +58,7 @@ protected function isTargeted(ReflectionInvocation $invocation, \ReflectionFunct

try {
$reflection = $this->classReflection($invocation->getClass());
} catch (LocatorException $e) {
} catch (LocatorException) {
return false;
}

Expand All @@ -79,7 +69,7 @@ protected function isTargeted(ReflectionInvocation $invocation, \ReflectionFunct

if ($target->isTrait()) {
//Let's compare traits
return in_array($target->getName(), $this->fetchTraits($invocation->getClass()));
return \in_array($target->getName(), $this->fetchTraits($invocation->getClass()));
}

return $reflection->getName() == $target->getName() || $reflection->isSubclassOf($target);
Expand Down
7 changes: 0 additions & 7 deletions src/Tokenizer/src/InvocationsInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Tokenizer;
Expand Down
Loading