Skip to content

Commit

Permalink
[UiBundle] Introduce scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed Aug 24, 2017
1 parent b80ab40 commit 61856a8
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/UiBundle/Block/BlockEventListener.php
Expand Up @@ -29,15 +29,15 @@ final class BlockEventListener
/**
* @param string $template
*/
public function __construct($template)
public function __construct(string $template)
{
$this->template = $template;
}

/**
* @param BlockEvent $event
*/
public function onBlockEvent(BlockEvent $event)
public function onBlockEvent(BlockEvent $event): void
{
$block = new Block();
$block->setId(uniqid('', true));
Expand Down
10 changes: 5 additions & 5 deletions src/Sylius/Bundle/UiBundle/Controller/SecurityController.php
Expand Up @@ -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, [
Expand All @@ -81,15 +81,15 @@ 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.');
}

/**
* @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.');
}
Expand Down
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/UiBundle/Form/Type/SecurityLoginType.php
Expand Up @@ -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, [
Expand All @@ -46,7 +46,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'sylius_security_login';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/UiBundle/Menu/Event/MenuBuilderEvent.php
Expand Up @@ -45,15 +45,15 @@ public function __construct(FactoryInterface $factory, ItemInterface $menu)
/**
* @return FactoryInterface
*/
public function getFactory()
public function getFactory(): FactoryInterface
{
return $this->factory;
}

/**
* @return ItemInterface
*/
public function getMenu()
public function getMenu(): ItemInterface
{
return $this->menu;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/UiBundle/Twig/PercentageExtension.php
Expand Up @@ -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']),
Expand All @@ -33,7 +33,7 @@ public function getFilters()
*
* @return string
*/
public function getPercentage($number)
public function getPercentage(float $number): string
{
$percentage = $number * 100;

Expand Down
31 changes: 22 additions & 9 deletions src/Sylius/Bundle/UiBundle/Twig/SortByExtension.php
Expand Up @@ -25,30 +25,25 @@ class SortByExtension extends \Twig_Extension
/**
* {@inheritdoc}
*/
public function getFilters()
public function getFilters(): array
{
return [
new \Twig_Filter('sort_by', [$this, 'sortBy']),
];
}

/**
* @param array|Collection $array
* @param iterable $iterable
* @param string $field
* @param string $order
*
* @return array
*
* @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();
Expand All @@ -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.');
}
}
Expand Up @@ -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,
Expand All @@ -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');

Expand All @@ -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.'))
Expand Down
Expand Up @@ -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 %');
}
Expand Down
25 changes: 11 additions & 14 deletions src/Sylius/Bundle/UiBundle/spec/Twig/SortByExtensionSpec.php
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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,
Expand All @@ -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([]);
}
}

0 comments on commit 61856a8

Please sign in to comment.