From ffeaa37799154720acec9cf1f31795f1e8049c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 30 Nov 2023 14:59:02 +0100 Subject: [PATCH 1/3] Move some namespaces on reflection --- psalm.xml | 3 +- .../SyliusResourceExtension.php | 2 +- .../Routing/CrudRoutesAttributesLoader.php | 2 +- src/Bundle/Routing/RouteAttributesFactory.php | 2 +- src/Bundle/Routing/RoutesAttributesLoader.php | 2 +- src/Component/Reflection/ClassReflection.php | 55 ++------------ .../Tests/Reflection/ClassInfoTraitTest.php | 2 +- .../Tests/Reflection/ClassReflectionTest.php | 2 +- .../spec/Reflection/ClassReflectionSpec.php | 25 +++++++ .../Common/State/PersistProcessor.php | 2 +- .../Doctrine/Common/State/RemoveProcessor.php | 2 +- ...butesResourceMetadataCollectionFactory.php | 2 +- .../Reflection/CallableReflection.php | 2 +- .../{ => src}/Reflection/ClassInfoTrait.php | 2 +- .../src/Reflection/ClassReflection.php | 72 +++++++++++++++++++ .../Filter/FunctionArgumentsFilter.php | 2 +- .../Request/RepositoryArgumentResolver.php | 2 +- .../src/Symfony/Request/State/Provider.php | 2 +- .../Reflection/CallableReflectionSpec.php | 4 +- .../Filter/FunctionArgumentsFilterSpec.php | 6 +- .../RepositoryArgumentResolverSpec.php | 2 +- 21 files changed, 123 insertions(+), 72 deletions(-) create mode 100644 src/Component/spec/Reflection/ClassReflectionSpec.php rename src/Component/{ => src}/Reflection/CallableReflection.php (95%) rename src/Component/{ => src}/Reflection/ClassInfoTrait.php (96%) create mode 100644 src/Component/src/Reflection/ClassReflection.php rename src/Component/{ => src}/Reflection/Filter/FunctionArgumentsFilter.php (94%) rename src/Component/{ => tests}/spec/Reflection/CallableReflectionSpec.php (91%) rename src/Component/{ => tests}/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php (82%) diff --git a/psalm.xml b/psalm.xml index be262e56e..b263f5175 100644 --- a/psalm.xml +++ b/psalm.xml @@ -134,7 +134,7 @@ - + @@ -192,6 +192,7 @@ + diff --git a/src/Bundle/DependencyInjection/SyliusResourceExtension.php b/src/Bundle/DependencyInjection/SyliusResourceExtension.php index 0f6d991d2..7d05dcad1 100644 --- a/src/Bundle/DependencyInjection/SyliusResourceExtension.php +++ b/src/Bundle/DependencyInjection/SyliusResourceExtension.php @@ -21,12 +21,12 @@ use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface; -use Sylius\Component\Resource\Reflection\ClassReflection; use Sylius\Resource\Factory\Factory; use Sylius\Resource\Factory\FactoryInterface; use Sylius\Resource\Metadata\AsResource; use Sylius\Resource\Metadata\Metadata; use Sylius\Resource\Metadata\ResourceMetadata; +use Sylius\Resource\Reflection\ClassReflection; use Sylius\Resource\State\ProcessorInterface; use Sylius\Resource\State\ProviderInterface; use Sylius\Resource\State\ResponderInterface; diff --git a/src/Bundle/Routing/CrudRoutesAttributesLoader.php b/src/Bundle/Routing/CrudRoutesAttributesLoader.php index a309d58ea..cdcc3fc82 100644 --- a/src/Bundle/Routing/CrudRoutesAttributesLoader.php +++ b/src/Bundle/Routing/CrudRoutesAttributesLoader.php @@ -14,8 +14,8 @@ namespace Sylius\Bundle\ResourceBundle\Routing; use Sylius\Component\Resource\Annotation\SyliusCrudRoutes as LegacySyliusCrudRoutes; -use Sylius\Component\Resource\Reflection\ClassReflection; use Sylius\Resource\Annotation\SyliusCrudRoutes; +use Sylius\Resource\Reflection\ClassReflection; use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Yaml\Yaml; diff --git a/src/Bundle/Routing/RouteAttributesFactory.php b/src/Bundle/Routing/RouteAttributesFactory.php index fc0a0023f..d5272a90b 100644 --- a/src/Bundle/Routing/RouteAttributesFactory.php +++ b/src/Bundle/Routing/RouteAttributesFactory.php @@ -14,8 +14,8 @@ namespace Sylius\Bundle\ResourceBundle\Routing; use Sylius\Component\Resource\Annotation\SyliusRoute as LegacySyliusRoute; -use Sylius\Component\Resource\Reflection\ClassReflection; use Sylius\Resource\Annotation\SyliusRoute; +use Sylius\Resource\Reflection\ClassReflection; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Webmozart\Assert\Assert; diff --git a/src/Bundle/Routing/RoutesAttributesLoader.php b/src/Bundle/Routing/RoutesAttributesLoader.php index 456f220f5..c49ca3cb5 100644 --- a/src/Bundle/Routing/RoutesAttributesLoader.php +++ b/src/Bundle/Routing/RoutesAttributesLoader.php @@ -13,7 +13,7 @@ namespace Sylius\Bundle\ResourceBundle\Routing; -use Sylius\Component\Resource\Reflection\ClassReflection; +use Sylius\Resource\Reflection\ClassReflection; use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface; use Symfony\Component\Routing\RouteCollection; diff --git a/src/Component/Reflection/ClassReflection.php b/src/Component/Reflection/ClassReflection.php index 7cc778825..6fd2f4610 100644 --- a/src/Component/Reflection/ClassReflection.php +++ b/src/Component/Reflection/ClassReflection.php @@ -13,58 +13,11 @@ namespace Sylius\Component\Resource\Reflection; -use Symfony\Component\Finder\Finder; +class_exists(\Sylius\Resource\Reflection\ClassReflection::class); -final class ClassReflection -{ - public static function getResourcesByPaths(array $paths): iterable +if (false) { + final class ClassReflection extends \Sylius\Resource\Reflection\ClassReflection { - foreach ($paths as $resourceDirectory) { - $resources = self::getResourcesByPath($resourceDirectory); - - foreach ($resources as $className) { - yield $className; - } - } - } - - public static function getResourcesByPath(string $path): iterable - { - $finder = new Finder(); - $finder->files()->in($path)->name('*.php')->sortByName(true); - - foreach ($finder as $file) { - $fileContent = (string) file_get_contents((string) $file->getRealPath()); - - preg_match('/namespace (.+);/', $fileContent, $matches); - - $namespace = $matches[1] ?? null; - - if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) { - // no class found - continue; - } - - $className = trim($matches[1]); - - if (null !== $namespace) { - yield $namespace . '\\' . $className; - } else { - yield $className; - } - } - } - - /** - * @psalm-param class-string $className - * - * @return \ReflectionAttribute[] - */ - public static function getClassAttributes(string $className, ?string $attributeName = null): array - { - $reflectionClass = new \ReflectionClass($className); - - /** @psalm-suppress ArgumentTypeCoercion */ - return $reflectionClass->getAttributes($attributeName); } } + diff --git a/src/Component/Tests/Reflection/ClassInfoTraitTest.php b/src/Component/Tests/Reflection/ClassInfoTraitTest.php index d0f52edd3..1a98b5e82 100644 --- a/src/Component/Tests/Reflection/ClassInfoTraitTest.php +++ b/src/Component/Tests/Reflection/ClassInfoTraitTest.php @@ -15,7 +15,7 @@ use App\Entity\Book; use PHPUnit\Framework\TestCase; -use Sylius\Component\Resource\Reflection\ClassInfoTrait; +use Sylius\Resource\Reflection\ClassInfoTrait; final class ClassInfoTraitTest extends TestCase { diff --git a/src/Component/Tests/Reflection/ClassReflectionTest.php b/src/Component/Tests/Reflection/ClassReflectionTest.php index 6b1f4f7d1..c9c36f372 100644 --- a/src/Component/Tests/Reflection/ClassReflectionTest.php +++ b/src/Component/Tests/Reflection/ClassReflectionTest.php @@ -17,12 +17,12 @@ use App\Entity\Route\ShowBook; use App\Entity\Route\ShowBookWithPriority; use PHPUnit\Framework\TestCase; -use Sylius\Component\Resource\Reflection\ClassReflection; use Sylius\Component\Resource\Tests\Dummy\DummyClassOne; use Sylius\Component\Resource\Tests\Dummy\DummyClassTwo; use Sylius\Component\Resource\Tests\Dummy\TraitPass; use Sylius\Resource\Annotation\SyliusCrudRoutes; use Sylius\Resource\Annotation\SyliusRoute; +use Sylius\Resource\Reflection\ClassReflection; final class ClassReflectionTest extends TestCase { diff --git a/src/Component/spec/Reflection/ClassReflectionSpec.php b/src/Component/spec/Reflection/ClassReflectionSpec.php new file mode 100644 index 000000000..a50b07d53 --- /dev/null +++ b/src/Component/spec/Reflection/ClassReflectionSpec.php @@ -0,0 +1,25 @@ +shouldBeAnInstanceOf(NewClassReflection::class); + } +} diff --git a/src/Component/src/Doctrine/Common/State/PersistProcessor.php b/src/Component/src/Doctrine/Common/State/PersistProcessor.php index a9ec82308..302e6182e 100644 --- a/src/Component/src/Doctrine/Common/State/PersistProcessor.php +++ b/src/Component/src/Doctrine/Common/State/PersistProcessor.php @@ -17,9 +17,9 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo; use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectManager as DoctrineObjectManager; -use Sylius\Component\Resource\Reflection\ClassInfoTrait; use Sylius\Resource\Context\Context; use Sylius\Resource\Metadata\Operation; +use Sylius\Resource\Reflection\ClassInfoTrait; use Sylius\Resource\State\ProcessorInterface; final class PersistProcessor implements ProcessorInterface diff --git a/src/Component/src/Doctrine/Common/State/RemoveProcessor.php b/src/Component/src/Doctrine/Common/State/RemoveProcessor.php index 4177ce148..d18b1d19a 100644 --- a/src/Component/src/Doctrine/Common/State/RemoveProcessor.php +++ b/src/Component/src/Doctrine/Common/State/RemoveProcessor.php @@ -15,9 +15,9 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectManager as DoctrineObjectManager; -use Sylius\Component\Resource\Reflection\ClassInfoTrait; use Sylius\Resource\Context\Context; use Sylius\Resource\Metadata\Operation; +use Sylius\Resource\Reflection\ClassInfoTrait; use Sylius\Resource\State\ProcessorInterface; final class RemoveProcessor implements ProcessorInterface diff --git a/src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php b/src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php index c90d3e7cb..63973e3b0 100644 --- a/src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php +++ b/src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php @@ -13,7 +13,6 @@ namespace Sylius\Resource\Metadata\Resource\Factory; -use Sylius\Component\Resource\Reflection\ClassReflection; use Sylius\Resource\Metadata\AsResource; use Sylius\Resource\Metadata\HttpOperation; use Sylius\Resource\Metadata\MetadataInterface; @@ -22,6 +21,7 @@ use Sylius\Resource\Metadata\RegistryInterface; use Sylius\Resource\Metadata\Resource\ResourceMetadataCollection; use Sylius\Resource\Metadata\ResourceMetadata; +use Sylius\Resource\Reflection\ClassReflection; use Sylius\Resource\Symfony\Request\State\Responder; use Sylius\Resource\Symfony\Routing\Factory\OperationRouteNameFactory; diff --git a/src/Component/Reflection/CallableReflection.php b/src/Component/src/Reflection/CallableReflection.php similarity index 95% rename from src/Component/Reflection/CallableReflection.php rename to src/Component/src/Reflection/CallableReflection.php index 8f0f3a1e8..31e63599a 100644 --- a/src/Component/Reflection/CallableReflection.php +++ b/src/Component/src/Reflection/CallableReflection.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Sylius\Component\Resource\Reflection; +namespace Sylius\Resource\Reflection; final class CallableReflection { diff --git a/src/Component/Reflection/ClassInfoTrait.php b/src/Component/src/Reflection/ClassInfoTrait.php similarity index 96% rename from src/Component/Reflection/ClassInfoTrait.php rename to src/Component/src/Reflection/ClassInfoTrait.php index e516afb9e..1ce38e809 100644 --- a/src/Component/Reflection/ClassInfoTrait.php +++ b/src/Component/src/Reflection/ClassInfoTrait.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Sylius\Component\Resource\Reflection; +namespace Sylius\Resource\Reflection; /** * Retrieves information about a class. diff --git a/src/Component/src/Reflection/ClassReflection.php b/src/Component/src/Reflection/ClassReflection.php new file mode 100644 index 000000000..730a81875 --- /dev/null +++ b/src/Component/src/Reflection/ClassReflection.php @@ -0,0 +1,72 @@ +files()->in($path)->name('*.php')->sortByName(true); + + foreach ($finder as $file) { + $fileContent = (string) file_get_contents((string) $file->getRealPath()); + + preg_match('/namespace (.+);/', $fileContent, $matches); + + $namespace = $matches[1] ?? null; + + if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) { + // no class found + continue; + } + + $className = trim($matches[1]); + + if (null !== $namespace) { + yield $namespace . '\\' . $className; + } else { + yield $className; + } + } + } + + /** + * @psalm-param class-string $className + * + * @return \ReflectionAttribute[] + */ + public static function getClassAttributes(string $className, ?string $attributeName = null): array + { + $reflectionClass = new \ReflectionClass($className); + + /** @psalm-suppress ArgumentTypeCoercion */ + return $reflectionClass->getAttributes($attributeName); + } +} + +class_alias(ClassReflection::class, \Sylius\Component\Resource\Reflection\ClassReflection::class); diff --git a/src/Component/Reflection/Filter/FunctionArgumentsFilter.php b/src/Component/src/Reflection/Filter/FunctionArgumentsFilter.php similarity index 94% rename from src/Component/Reflection/Filter/FunctionArgumentsFilter.php rename to src/Component/src/Reflection/Filter/FunctionArgumentsFilter.php index 364936d45..f4957e7fc 100644 --- a/src/Component/Reflection/Filter/FunctionArgumentsFilter.php +++ b/src/Component/src/Reflection/Filter/FunctionArgumentsFilter.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Sylius\Component\Resource\Reflection\Filter; +namespace Sylius\Resource\Reflection\Filter; final class FunctionArgumentsFilter { diff --git a/src/Component/src/Symfony/Request/RepositoryArgumentResolver.php b/src/Component/src/Symfony/Request/RepositoryArgumentResolver.php index 405520cee..e893089b6 100644 --- a/src/Component/src/Symfony/Request/RepositoryArgumentResolver.php +++ b/src/Component/src/Symfony/Request/RepositoryArgumentResolver.php @@ -13,7 +13,7 @@ namespace Sylius\Resource\Symfony\Request; -use Sylius\Component\Resource\Reflection\Filter\FunctionArgumentsFilter; +use Sylius\Resource\Reflection\Filter\FunctionArgumentsFilter; use Symfony\Component\HttpFoundation\Request; final class RepositoryArgumentResolver diff --git a/src/Component/src/Symfony/Request/State/Provider.php b/src/Component/src/Symfony/Request/State/Provider.php index d7b1b2fd5..fe7fdd26d 100644 --- a/src/Component/src/Symfony/Request/State/Provider.php +++ b/src/Component/src/Symfony/Request/State/Provider.php @@ -15,12 +15,12 @@ use Pagerfanta\Pagerfanta; use Psr\Container\ContainerInterface; -use Sylius\Component\Resource\Reflection\CallableReflection; use Sylius\Resource\Context\Context; use Sylius\Resource\Context\Option\RequestOption; use Sylius\Resource\Metadata\BulkOperationInterface; use Sylius\Resource\Metadata\CollectionOperationInterface; use Sylius\Resource\Metadata\Operation; +use Sylius\Resource\Reflection\CallableReflection; use Sylius\Resource\State\ProviderInterface; use Sylius\Resource\Symfony\ExpressionLanguage\ArgumentParserInterface; use Sylius\Resource\Symfony\Request\RepositoryArgumentResolver; diff --git a/src/Component/spec/Reflection/CallableReflectionSpec.php b/src/Component/tests/spec/Reflection/CallableReflectionSpec.php similarity index 91% rename from src/Component/spec/Reflection/CallableReflectionSpec.php rename to src/Component/tests/spec/Reflection/CallableReflectionSpec.php index 63e12999d..fd8dbc3f4 100644 --- a/src/Component/spec/Reflection/CallableReflectionSpec.php +++ b/src/Component/tests/spec/Reflection/CallableReflectionSpec.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace spec\Sylius\Component\Resource\Reflection; +namespace spec\Sylius\Resource\Reflection; use PhpSpec\ObjectBehavior; -use Sylius\Component\Resource\Reflection\CallableReflection; use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables; +use Sylius\Resource\Reflection\CallableReflection; class CallableReflectionSpec extends ObjectBehavior { diff --git a/src/Component/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php b/src/Component/tests/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php similarity index 82% rename from src/Component/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php rename to src/Component/tests/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php index d7a2748a9..162a450fb 100644 --- a/src/Component/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php +++ b/src/Component/tests/spec/Reflection/Filter/FunctionArgumentsFilterSpec.php @@ -11,12 +11,12 @@ declare(strict_types=1); -namespace spec\Sylius\Component\Resource\Reflection\Filter; +namespace spec\Sylius\Resource\Reflection\Filter; use PhpSpec\ObjectBehavior; -use Sylius\Component\Resource\Reflection\CallableReflection; -use Sylius\Component\Resource\Reflection\Filter\FunctionArgumentsFilter; use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables; +use Sylius\Resource\Reflection\CallableReflection; +use Sylius\Resource\Reflection\Filter\FunctionArgumentsFilter; final class FunctionArgumentsFilterSpec extends ObjectBehavior { diff --git a/src/Component/tests/spec/Symfony/Request/RepositoryArgumentResolverSpec.php b/src/Component/tests/spec/Symfony/Request/RepositoryArgumentResolverSpec.php index d3b9f12c9..c6417fae6 100644 --- a/src/Component/tests/spec/Symfony/Request/RepositoryArgumentResolverSpec.php +++ b/src/Component/tests/spec/Symfony/Request/RepositoryArgumentResolverSpec.php @@ -14,8 +14,8 @@ namespace spec\Sylius\Resource\Symfony\Request; use PhpSpec\ObjectBehavior; -use Sylius\Component\Resource\Reflection\CallableReflection; use Sylius\Component\Resource\Tests\Dummy\RepositoryWithCallables; +use Sylius\Resource\Reflection\CallableReflection; use Sylius\Resource\Symfony\Request\RepositoryArgumentResolver; use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\HttpFoundation\ParameterBag; From b98ab4426f09895f12410a494a98e6a42f632952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 12 Dec 2023 09:25:04 +0100 Subject: [PATCH 2/3] Apply suggestions from code review --- src/Component/src/Reflection/ClassReflection.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Component/src/Reflection/ClassReflection.php b/src/Component/src/Reflection/ClassReflection.php index 730a81875..46f7be46a 100644 --- a/src/Component/src/Reflection/ClassReflection.php +++ b/src/Component/src/Reflection/ClassReflection.php @@ -34,7 +34,10 @@ public static function getResourcesByPath(string $path): iterable $finder->files()->in($path)->name('*.php')->sortByName(true); foreach ($finder as $file) { - $fileContent = (string) file_get_contents((string) $file->getRealPath()); + $fileContent = file_get_contents((string) $file->getRealPath()); + if (false === $fileContent) { + throw new \RuntimeException(sprintf('Unable to read "%s" file', $file->getRealPath())); + } preg_match('/namespace (.+);/', $fileContent, $matches); From cea38d6841d006f5e6e94b7c2916d02a1e329ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 12 Dec 2023 09:27:10 +0100 Subject: [PATCH 3/3] Remove an unnecessary empty line --- src/Component/Reflection/ClassReflection.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Component/Reflection/ClassReflection.php b/src/Component/Reflection/ClassReflection.php index 6fd2f4610..e8a9e560a 100644 --- a/src/Component/Reflection/ClassReflection.php +++ b/src/Component/Reflection/ClassReflection.php @@ -20,4 +20,3 @@ final class ClassReflection extends \Sylius\Resource\Reflection\ClassReflection { } } -