From 61856a8e8e622b749583957368d54c870696edf3 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 24 Aug 2017 12:55:22 +0200 Subject: [PATCH] [UiBundle] Introduce scalar types --- .../UiBundle/Block/BlockEventListener.php | 4 +-- .../Controller/SecurityController.php | 10 +++--- .../DependencyInjection/SyliusUiExtension.php | 2 +- .../UiBundle/Form/Type/SecurityLoginType.php | 4 +-- .../UiBundle/Menu/Event/MenuBuilderEvent.php | 4 +-- .../UiBundle/Twig/PercentageExtension.php | 4 +-- .../Bundle/UiBundle/Twig/SortByExtension.php | 31 +++++++++++++------ .../Controller/SecurityControllerSpec.php | 13 +++----- .../spec/Twig/PercentageExtensionSpec.php | 4 +-- .../spec/Twig/SortByExtensionSpec.php | 25 +++++++-------- 10 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/Sylius/Bundle/UiBundle/Block/BlockEventListener.php b/src/Sylius/Bundle/UiBundle/Block/BlockEventListener.php index 3739337d6f6..bccc100d124 100644 --- a/src/Sylius/Bundle/UiBundle/Block/BlockEventListener.php +++ b/src/Sylius/Bundle/UiBundle/Block/BlockEventListener.php @@ -29,7 +29,7 @@ final class BlockEventListener /** * @param string $template */ - public function __construct($template) + public function __construct(string $template) { $this->template = $template; } @@ -37,7 +37,7 @@ public function __construct($template) /** * @param BlockEvent $event */ - public function onBlockEvent(BlockEvent $event) + public function onBlockEvent(BlockEvent $event): void { $block = new Block(); $block->setId(uniqid('', true)); diff --git a/src/Sylius/Bundle/UiBundle/Controller/SecurityController.php b/src/Sylius/Bundle/UiBundle/Controller/SecurityController.php index fa0d91e30f5..0a332846939 100644 --- a/src/Sylius/Bundle/UiBundle/Controller/SecurityController.php +++ b/src/Sylius/Bundle/UiBundle/Controller/SecurityController.php @@ -60,15 +60,15 @@ public function __construct( * * @return Response */ - public function loginAction(Request $request) + public function loginAction(Request $request): Response { $lastError = $this->authenticationUtils->getLastAuthenticationError(); $lastUsername = $this->authenticationUtils->getLastUsername(); $options = $request->attributes->get('_sylius'); - $template = isset($options['template']) ? $options['template'] : '@SyliusUi/Security/login.html.twig'; - $formType = isset($options['form']) ? $options['form'] : SecurityLoginType::class; + $template = $options['template'] ?? '@SyliusUi/Security/login.html.twig'; + $formType = $options['form'] ?? SecurityLoginType::class; $form = $this->formFactory->createNamed('', $formType); return $this->templatingEngine->renderResponse($template, [ @@ -81,7 +81,7 @@ public function loginAction(Request $request) /** * @param Request $request */ - public function checkAction(Request $request) + public function checkAction(Request $request): void { throw new \RuntimeException('You must configure the check path to be handled by the firewall.'); } @@ -89,7 +89,7 @@ public function checkAction(Request $request) /** * @param Request $request */ - public function logoutAction(Request $request) + public function logoutAction(Request $request): void { throw new \RuntimeException('You must configure the logout path to be handled by the firewall.'); } diff --git a/src/Sylius/Bundle/UiBundle/DependencyInjection/SyliusUiExtension.php b/src/Sylius/Bundle/UiBundle/DependencyInjection/SyliusUiExtension.php index ca8825258e8..109507c425f 100644 --- a/src/Sylius/Bundle/UiBundle/DependencyInjection/SyliusUiExtension.php +++ b/src/Sylius/Bundle/UiBundle/DependencyInjection/SyliusUiExtension.php @@ -26,7 +26,7 @@ final class SyliusUiExtension extends Extension /** * {@inheritdoc} */ - public function load(array $config, ContainerBuilder $container) + public function load(array $config, ContainerBuilder $container): void { $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.xml'); diff --git a/src/Sylius/Bundle/UiBundle/Form/Type/SecurityLoginType.php b/src/Sylius/Bundle/UiBundle/Form/Type/SecurityLoginType.php index 875fb90ee29..83bc6f83d5b 100644 --- a/src/Sylius/Bundle/UiBundle/Form/Type/SecurityLoginType.php +++ b/src/Sylius/Bundle/UiBundle/Form/Type/SecurityLoginType.php @@ -27,7 +27,7 @@ final class SecurityLoginType extends AbstractType /** * {@inheritdoc} */ - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('_username', TextType::class, [ @@ -46,7 +46,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) /** * {@inheritdoc} */ - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sylius_security_login'; } diff --git a/src/Sylius/Bundle/UiBundle/Menu/Event/MenuBuilderEvent.php b/src/Sylius/Bundle/UiBundle/Menu/Event/MenuBuilderEvent.php index a9a6e413f98..4e56e16e40c 100644 --- a/src/Sylius/Bundle/UiBundle/Menu/Event/MenuBuilderEvent.php +++ b/src/Sylius/Bundle/UiBundle/Menu/Event/MenuBuilderEvent.php @@ -45,7 +45,7 @@ public function __construct(FactoryInterface $factory, ItemInterface $menu) /** * @return FactoryInterface */ - public function getFactory() + public function getFactory(): FactoryInterface { return $this->factory; } @@ -53,7 +53,7 @@ public function getFactory() /** * @return ItemInterface */ - public function getMenu() + public function getMenu(): ItemInterface { return $this->menu; } diff --git a/src/Sylius/Bundle/UiBundle/Twig/PercentageExtension.php b/src/Sylius/Bundle/UiBundle/Twig/PercentageExtension.php index 504ac3fdca9..6e989c4f3e5 100644 --- a/src/Sylius/Bundle/UiBundle/Twig/PercentageExtension.php +++ b/src/Sylius/Bundle/UiBundle/Twig/PercentageExtension.php @@ -21,7 +21,7 @@ class PercentageExtension extends \Twig_Extension /** * {@inheritdoc} */ - public function getFilters() + public function getFilters(): array { return [ new \Twig_Filter('sylius_percentage', [$this, 'getPercentage']), @@ -33,7 +33,7 @@ public function getFilters() * * @return string */ - public function getPercentage($number) + public function getPercentage(float $number): string { $percentage = $number * 100; diff --git a/src/Sylius/Bundle/UiBundle/Twig/SortByExtension.php b/src/Sylius/Bundle/UiBundle/Twig/SortByExtension.php index 43d5a194690..992b89cbe31 100644 --- a/src/Sylius/Bundle/UiBundle/Twig/SortByExtension.php +++ b/src/Sylius/Bundle/UiBundle/Twig/SortByExtension.php @@ -25,7 +25,7 @@ class SortByExtension extends \Twig_Extension /** * {@inheritdoc} */ - public function getFilters() + public function getFilters(): array { return [ new \Twig_Filter('sort_by', [$this, 'sortBy']), @@ -33,7 +33,7 @@ public function getFilters() } /** - * @param array|Collection $array + * @param iterable $iterable * @param string $field * @param string $order * @@ -41,14 +41,9 @@ public function getFilters() * * @throws NoSuchPropertyException */ - public function sortBy($array, $field, $order = 'ASC') + public function sortBy(iterable $iterable, string $field, string $order = 'ASC'): array { - if ($array instanceof Collection) { - $array = $array->toArray(); - } - if (1 >= count($array)) { - return $array; - } + $array = $this->transformIterableToArray($iterable); usort($array, function ($firstElement, $secondElement) use ($field, $order) { $accessor = PropertyAccess::createPropertyAccessor(); @@ -66,4 +61,22 @@ public function sortBy($array, $field, $order = 'ASC') return $array; } + + /** + * @param iterable $iterable + * + * @return array + */ + private function transformIterableToArray(iterable $iterable): array + { + if (is_array($iterable)) { + return $iterable; + } + + if ($iterable instanceof \Traversable) { + return iterator_to_array($iterable); + } + + throw new \RuntimeException('Cannot transform an iterable to an array.'); + } } diff --git a/src/Sylius/Bundle/UiBundle/spec/Controller/SecurityControllerSpec.php b/src/Sylius/Bundle/UiBundle/spec/Controller/SecurityControllerSpec.php index 9eb444e27c4..a5a0ff57581 100644 --- a/src/Sylius/Bundle/UiBundle/spec/Controller/SecurityControllerSpec.php +++ b/src/Sylius/Bundle/UiBundle/spec/Controller/SecurityControllerSpec.php @@ -29,16 +29,11 @@ */ final class SecurityControllerSpec extends ObjectBehavior { - function let(AuthenticationUtils $authenticationUtils, FormFactoryInterface $formFactory, EngineInterface $templatingEngine) + function let(AuthenticationUtils $authenticationUtils, FormFactoryInterface $formFactory, EngineInterface $templatingEngine): void { $this->beConstructedWith($authenticationUtils, $formFactory, $templatingEngine); } - function it_is_initializable() - { - $this->shouldHaveType(SecurityController::class); - } - function it_renders_login_form( Request $request, ParameterBag $requestAttributes, @@ -48,7 +43,7 @@ function it_renders_login_form( FormView $formView, EngineInterface $templatingEngine, Response $response - ) { + ): void { $authenticationUtils->getLastAuthenticationError()->willReturn('Bad credentials.'); $authenticationUtils->getLastUsername()->willReturn('john.doe'); @@ -73,14 +68,14 @@ function it_renders_login_form( $this->loginAction($request)->shouldReturn($response); } - function it_throws_an_exception_when_check_action_is_accessed(Request $request) + function it_throws_an_exception_when_check_action_is_accessed(Request $request): void { $this ->shouldThrow(new \RuntimeException('You must configure the check path to be handled by the firewall.')) ->during('checkAction', [$request]); } - function it_throws_an_exception_when_logout_action_is_accessed(Request $request) + function it_throws_an_exception_when_logout_action_is_accessed(Request $request): void { $this ->shouldThrow(new \RuntimeException('You must configure the logout path to be handled by the firewall.')) diff --git a/src/Sylius/Bundle/UiBundle/spec/Twig/PercentageExtensionSpec.php b/src/Sylius/Bundle/UiBundle/spec/Twig/PercentageExtensionSpec.php index d90f33b25a4..600e62d6ab4 100644 --- a/src/Sylius/Bundle/UiBundle/spec/Twig/PercentageExtensionSpec.php +++ b/src/Sylius/Bundle/UiBundle/spec/Twig/PercentageExtensionSpec.php @@ -21,12 +21,12 @@ */ final class PercentageExtensionSpec extends ObjectBehavior { - function it_is_twig_extension() + function it_is_twig_extension(): void { $this->shouldHaveType(\Twig_Extension::class); } - function it_returns_float_number_as_percentage() + function it_returns_float_number_as_percentage(): void { $this->getPercentage(0.112)->shouldReturn('11.2 %'); } diff --git a/src/Sylius/Bundle/UiBundle/spec/Twig/SortByExtensionSpec.php b/src/Sylius/Bundle/UiBundle/spec/Twig/SortByExtensionSpec.php index 316e7cf58ff..38710e5b54b 100644 --- a/src/Sylius/Bundle/UiBundle/spec/Twig/SortByExtensionSpec.php +++ b/src/Sylius/Bundle/UiBundle/spec/Twig/SortByExtensionSpec.php @@ -13,9 +13,8 @@ namespace spec\Sylius\Bundle\UiBundle\Twig; -use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\ArrayCollection; use PhpSpec\ObjectBehavior; -use Sylius\Bundle\UiBundle\Twig\SortByExtension; use Sylius\Bundle\UiBundle\spec\Fixtures\SampleInterface; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; @@ -24,7 +23,7 @@ */ final class SortByExtensionSpec extends ObjectBehavior { - function it_extends_twig_extensions() + function it_extends_twig_extensions(): void { $this->shouldHaveType(\Twig_Extension::class); } @@ -33,7 +32,7 @@ function it_sorts_in_ascending_order_by_default( SampleInterface $firstSample, SampleInterface $secondSample, SampleInterface $thirdSample - ) { + ): void { $firstSample->getInt()->willReturn(3); $secondSample->getInt()->willReturn(5); $thirdSample->getInt()->willReturn(1); @@ -57,7 +56,7 @@ function it_sorts_an_array_of_objects_by_various_properties( SampleInterface $firstSample, SampleInterface $secondSample, SampleInterface $thirdSample - ) { + ): void { $firstSample->getInt()->willReturn(3); $secondSample->getInt()->willReturn(5); $thirdSample->getInt()->willReturn(1); @@ -105,7 +104,7 @@ function it_sorts_an_array_of_objects_in_descending_order_by_a_property( SampleInterface $firstSample, SampleInterface $secondSample, SampleInterface $thirdSample - ) { + ): void { $firstSample->getInt()->willReturn(3); $secondSample->getInt()->willReturn(5); $thirdSample->getInt()->willReturn(1); @@ -132,7 +131,7 @@ function it_sorts_an_array_of_objects_by_a_nested_property( SampleInterface $firstInnerSample, SampleInterface $secondInnerSample, SampleInterface $thirdInnerSample - ) { + ): void { $firstInnerSample->getString()->willReturn('m'); $secondInnerSample->getString()->willReturn('Z'); $thirdInnerSample->getString()->willReturn('A'); @@ -160,7 +159,7 @@ function it_throws_an_exception_if_the_property_is_not_found_on_objects( SampleInterface $firstSample, SampleInterface $secondSample, SampleInterface $thirdSample - ) { + ): void { $arrayBeforeSorting = [ $firstSample, $secondSample, @@ -173,20 +172,18 @@ function it_throws_an_exception_if_the_property_is_not_found_on_objects( ; } - function it_return_input_array_if_there_is_only_one_object_inside(SampleInterface $sample) + function it_return_input_array_if_there_is_only_one_object_inside(SampleInterface $sample): void { $this->sortBy([$sample], 'property')->shouldReturn([$sample]); } - function it_does_nothing_if_array_is_empty() + function it_does_nothing_if_array_is_empty(): void { $this->sortBy([], 'property')->shouldReturn([]); } - function it_does_nothing_if_collection_is_empty(Collection $collection) + function it_does_nothing_if_collection_is_empty(): void { - $collection->toArray()->willReturn([]); - - $this->sortBy($collection, 'property')->shouldReturn([]); + $this->sortBy(new ArrayCollection(), 'property')->shouldReturn([]); } }