diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..26a8eb6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +; top-most EditorConfig file +root = true + +; Unix-style newlines +[*] +charset = utf-8 +end_of_line = LF +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{php,html,twig}] +indent_style = space +indent_size = 4 + +[*.md] +max_line_length = 80 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e5bf6b5..9a3ad52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,3 +61,4 @@ jobs: - name: Run tests run: make test-lowest + diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..e914a6a --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,24 @@ +setRules([ + '@PHP71Migration' => true, + '@PHPUnit75Migration:risky' => true, + '@Symfony' => true, + '@Symfony:risky' => true, + 'protected_to_private' => false, + 'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => false], + ]) + ->setRiskyAllowed(true) + ->setFinder( + (new PhpCsFixer\Finder()) + ->in(__DIR__.'/src') + ->notPath('#/Fixtures/#') + ->in(__DIR__.'/tests') + ->append([__FILE__]) + ) +; diff --git a/Makefile b/Makefile index fb79a3c..c1b2c80 100644 --- a/Makefile +++ b/Makefile @@ -18,3 +18,6 @@ test-php73-lowest: test-php74-lowest: docker run --rm -v $(DIR):/project -w /project webdevops/php:7.4 $(MAKE) test-lowest + +cs: + docker run --rm -v $(DIR):/project -w /project jakzal/phpqa php-cs-fixer fix diff --git a/README.md b/README.md index a14b3f2..5f14933 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,36 @@ Getting Started With BreadcrumbTrailBundle ========================================== -This bundle provides a breacrumb trail service also known as breadcrumbs or Fil d'Ariane. -Breadcrumbs can be defined with annotations, PHP and Twig. +This bundle provides a breadcrumb trail service also known as breadcrumbs or Fil d'Ariane. +Breadcrumbs can be defined with Attributes, annotations, PHP and Twig. ## Installation Please follow the steps given in [installation.md](src/Resources/doc/installation.md) to install this bundle. -## Summary +## Bundle documentation - - [Annotation configuration](src/Resources/doc/annotation_configuration.md) - - [PHP configuration](src/Resources/doc/php_configuration.md) - - [Twig configuration](src/Resources/doc/twig_configuration.md) - - [Render the breadcrumb trail](src/Resources/doc/rendering.md) - - [Override the template](src/Resources/doc/override_template.md) +- [Annotation configuration](src/Resources/doc/annotation_configuration.md) +- [PHP configuration](src/Resources/doc/php_configuration.md) +- [Twig configuration](src/Resources/doc/twig_configuration.md) +- [Render the breadcrumb trail](src/Resources/doc/rendering.md) +- [Override the template](src/Resources/doc/override_template.md) + +## Tests + +Several make targets can get used to run the PHPUnit test suite on different PHP environments: + +``` +$ make test +$ make test-php73 +$ make test-php74-lowest +``` + +## Code style + +PHP-CS-Fixer is used to keep the code style in shape. There is a make target that uses Docker to fix +the code style without having to install any other dependencies: + +``` +$ make cs +``` diff --git a/src/Annotation/Breadcrumb.php b/src/Annotation/Breadcrumb.php index bb4ac8e..562a917 100644 --- a/src/Annotation/Breadcrumb.php +++ b/src/Annotation/Breadcrumb.php @@ -30,15 +30,15 @@ class Breadcrumb /** * @var mixed An array of parameters for the route */ - private $routeParameters = array(); + private $routeParameters = []; /** - * @var Boolean Whether to generate an absolute URL + * @var bool Whether to generate an absolute URL */ private $routeAbsolute = false; /** - * @var integer Position of the breadcrumb (default = 0) + * @var int Position of the breadcrumb (default = 0) */ private $position = 0; @@ -50,17 +50,17 @@ class Breadcrumb /** * @var mixed An array of additional attributes for the breadcrumb */ - private $attributes = array(); + private $attributes = []; /** - * @param array|string $title title, or the legacy array that contains all annotation data + * @param array|string $title title, or the legacy array that contains all annotation data * @param ?array $route - * @param ?string $routeName - * @param ?array $routeParameters - * @param bool $routeAbsolute - * @param int $position - * @param ?string $template - * @param array $attributes + * @param ?string $routeName + * @param ?array $routeParameters + * @param bool $routeAbsolute + * @param int $position + * @param ?string $template + * @param array $attributes */ public function __construct( $title, @@ -70,13 +70,12 @@ public function __construct( $position = null, $template = null, $attributes = null - ) - { + ) { $data = []; - if (is_string($title)) { - $data = ["title" => $title]; - } elseif (is_array($title)) { + if (\is_string($title)) { + $data = ['title' => $title]; + } elseif (\is_array($title)) { $data = $title; } @@ -99,16 +98,15 @@ public function __construct( } if (isset($data['route'])) { - if (is_array($data['route'])) { + if (\is_array($data['route'])) { foreach ($data['route'] as $key => $value) { $method = 'setRoute'.$key; if (!method_exists($this, $method)) { - throw new \BadMethodCallException(sprintf("Unknown property '%s' for the 'route' parameter on annotation '%s'.", $key, get_class($this))); + throw new \BadMethodCallException(sprintf("Unknown property '%s' for the 'route' parameter on annotation '%s'.", $key, static::class)); } $this->$method($value); } - } - else { + } else { $data['routeName'] = $data['route']; } @@ -116,15 +114,14 @@ public function __construct( } foreach ($data as $key => $value) { - // Do not attempt setting values that were provided as null - if ($value === null) { + if (null === $value) { continue; } $method = 'set'.$key; if (!method_exists($this, $method)) { - throw new \BadMethodCallException(sprintf("Unknown property '%s' on annotation '%s'.", $key, get_class($this))); + throw new \BadMethodCallException(sprintf("Unknown property '%s' on annotation '%s'.", $key, static::class)); } $this->$method($value); } @@ -146,7 +143,7 @@ public function getTitle() } /** - * Sets the name of the route + * Sets the name of the route. * * @param string $routeName The name of the route */ @@ -161,7 +158,7 @@ public function getRouteName() } /** - * Sets an array of parameters for the route + * Sets an array of parameters for the route. * * @param mixed $routeParameters An array of parameters for the route */ @@ -176,9 +173,9 @@ public function getRouteParameters() } /** - * Whether to generate an absolute URL + * Whether to generate an absolute URL. * - * @param Boolean $routeName Whether to generate an absolute URL + * @param bool $routeName Whether to generate an absolute URL */ public function setRouteAbsolute($routeAbsolute) { @@ -193,7 +190,7 @@ public function getRouteAbsolute() /** * Sets the position of the breadcrumb. * - * @param integer Position of the breadcrumb (default = 0) + * @param int Position of the breadcrumb (default = 0) */ public function setPosition($position) { @@ -208,7 +205,7 @@ public function getPosition() /** * Sets the template of the breadcrumb trail. * - * @param string Template of the breadcrumb trail. + * @param string template of the breadcrumb trail */ public function setTemplate($template) { @@ -223,7 +220,7 @@ public function getTemplate() /** * Sets the additional attributes for the breadcrumb. * - * @param mixed Additional attributes for the breadcrumb. + * @param mixed additional attributes for the breadcrumb */ public function setAttributes($attributes) { diff --git a/src/BreadcrumbTrail/Breadcrumb.php b/src/BreadcrumbTrail/Breadcrumb.php index 83a544a..1400ea8 100644 --- a/src/BreadcrumbTrail/Breadcrumb.php +++ b/src/BreadcrumbTrail/Breadcrumb.php @@ -31,11 +31,11 @@ class Breadcrumb /** * Constructor. * - * @param string $title Title of the breadcrumb - * @param string $url Url of the breadcrumb - * @param mixed $attributes Additional attributes for the breadcrumb + * @param string $title Title of the breadcrumb + * @param string $url Url of the breadcrumb + * @param mixed $attributes Additional attributes for the breadcrumb */ - public function __construct($title, $url = null, $attributes = array()) + public function __construct($title, $url = null, $attributes = []) { $this->title = $title; $this->url = $url; diff --git a/src/BreadcrumbTrail/Trail.php b/src/BreadcrumbTrail/Trail.php index 5b21295..ae2684a 100644 --- a/src/BreadcrumbTrail/Trail.php +++ b/src/BreadcrumbTrail/Trail.php @@ -59,72 +59,70 @@ public function getTemplate() } /** - * Add breadcrumb + * Add breadcrumb. + * + * @param mixed $breadcrumb_or_title A Breadcrumb instance or the title of the breadcrumb + * @param string $routeName The name of the route + * @param mixed $routeParameters An array of parameters for the route + * @param bool $routeAbsolute Whether to generate an absolute URL + * @param int $position Position of the breadcrumb (default = 0) + * @param mixed $attributes Additional attributes for the breadcrumb * - * @param mixed $breadcrumb_or_title A Breadcrumb instance or the title of the breadcrumb - * @param string $routeName The name of the route - * @param mixed $routeParameters An array of parameters for the route - * @param boolean $routeAbsolute Whether to generate an absolute URL - * @param integer $position Position of the breadcrumb (default = 0) - * @param mixed $attributes Additional attributes for the breadcrumb * @throws \RuntimeException * @throws \InvalidArgumentException + * * @return self */ - public function add($breadcrumb_or_title, $routeName = null, $routeParameters = array(), $routeAbsolute = UrlGeneratorInterface::ABSOLUTE_PATH, $position = 0, $attributes = array()) + public function add($breadcrumb_or_title, $routeName = null, $routeParameters = [], $routeAbsolute = UrlGeneratorInterface::ABSOLUTE_PATH, $position = 0, $attributes = []) { - if ($breadcrumb_or_title === null) { + if (null === $breadcrumb_or_title) { return $this->reset(); } if ($breadcrumb_or_title instanceof Breadcrumb) { $breadcrumb = $breadcrumb_or_title; } else { - if (!is_string($breadcrumb_or_title)) { + if (!\is_string($breadcrumb_or_title)) { throw new \InvalidArgumentException('The title of a breadcrumb must be a string.'); } $request = $this->requestStack->getCurrentRequest(); - if ($request !== null) { - preg_match_all('#\{(?P\w+).?(?P([\w\.])*):?(?P(\w|,| )*)\}#', $breadcrumb_or_title, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); + if (null !== $request) { + preg_match_all('#\{(?P\w+).?(?P([\w\.])*):?(?P(\w|,| )*)\}#', $breadcrumb_or_title, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER); foreach ($matches as $match) { - $varName = $match['variable'][0]; - $functions = $match['function'][0] ? explode('.', $match['function'][0]) : array(); - $parameters = $match['parameters'][0] ? explode(',', $match['parameters'][0]) : array(); - $nbCalls = count($functions); + $varName = $match['variable'][0]; + $functions = $match['function'][0] ? explode('.', $match['function'][0]) : []; + $parameters = $match['parameters'][0] ? explode(',', $match['parameters'][0]) : []; + $nbCalls = \count($functions); - if($request->attributes->has($varName)) { + if ($request->attributes->has($varName)) { $object = $request->attributes->get($varName); - if(empty($functions)) { + if (empty($functions)) { $objectValue = (string) $object; - } - else { - foreach ($functions AS $f => $function) { - - # While this is not the last function, call the chain + } else { + foreach ($functions as $f => $function) { + // While this is not the last function, call the chain if ($f < $nbCalls - 1) { - if(is_callable(array($object, $fullFunctionName = 'get'.$function)) - || is_callable(array($object, $fullFunctionName = 'has'.$function)) - || is_callable(array($object, $fullFunctionName = 'is'.$function))) { - $object = call_user_func(array($object, $fullFunctionName)); - } - else { - throw new \RuntimeException(sprintf('"%s" is not callable.', join('.', array_merge([$varName], $functions)))); + if (\is_callable([$object, $fullFunctionName = 'get'.$function]) + || \is_callable([$object, $fullFunctionName = 'has'.$function]) + || \is_callable([$object, $fullFunctionName = 'is'.$function])) { + $object = \call_user_func([$object, $fullFunctionName]); + } else { + throw new \RuntimeException(sprintf('"%s" is not callable.', implode('.', array_merge([$varName], $functions)))); } } - # End of the chain: call the method + // End of the chain: call the method else { - if(is_callable(array($object, $fullFunctionName = 'get'.$function)) - || is_callable(array($object, $fullFunctionName = 'has'.$function)) - || is_callable(array($object, $fullFunctionName = 'is'.$function))) { - $objectValue = call_user_func_array(array($object, $fullFunctionName),$parameters); - } - else { - throw new \RuntimeException(sprintf('"%s" is not callable.', join('.', array_merge([$varName], $functions)))); + if (\is_callable([$object, $fullFunctionName = 'get'.$function]) + || \is_callable([$object, $fullFunctionName = 'has'.$function]) + || \is_callable([$object, $fullFunctionName = 'is'.$function])) { + $objectValue = \call_user_func_array([$object, $fullFunctionName], $parameters); + } else { + throw new \RuntimeException(sprintf('"%s" is not callable.', implode('.', array_merge([$varName], $functions)))); } } } @@ -139,58 +137,49 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters = $routeParameters[$value] = $request->get($value); unset($routeParameters[$key]); } else { - if (preg_match_all('#\{(?P\w+).?(?P([\w\.])*):?(?P(\w|,| )*)\}#', $value, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { - - foreach ($matches AS $match) { - - $varName = $match['variable'][0]; - $functions = $match['function'][0] ? explode('.', $match['function'][0]) : array(); - $parameters = $match['parameters'][0] ? explode(',', $match['parameters'][0]) : array(); - $nbCalls = count($functions); + if (preg_match_all('#\{(?P\w+).?(?P([\w\.])*):?(?P(\w|,| )*)\}#', $value, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER)) { + foreach ($matches as $match) { + $varName = $match['variable'][0]; + $functions = $match['function'][0] ? explode('.', $match['function'][0]) : []; + $parameters = $match['parameters'][0] ? explode(',', $match['parameters'][0]) : []; + $nbCalls = \count($functions); if ($request->attributes->has($varName)) { $object = $request->attributes->get($varName); if (empty($functions)) { $objectValue = (string) $object; - } - else { - foreach ($functions AS $f => $function) { - - # While this is not the last function, call the chain + } else { + foreach ($functions as $f => $function) { + // While this is not the last function, call the chain if ($f < $nbCalls - 1) { - if (is_callable(array($object, $fullFunctionName = 'get' . $function)) - || is_callable(array($object, $fullFunctionName = 'has' . $function)) - || is_callable(array($object, $fullFunctionName = 'is' . $function)) + if (\is_callable([$object, $fullFunctionName = 'get'.$function]) + || \is_callable([$object, $fullFunctionName = 'has'.$function]) + || \is_callable([$object, $fullFunctionName = 'is'.$function]) ) { - $object = call_user_func(array($object, $fullFunctionName)); - } - else { - throw new \RuntimeException(sprintf('"%s" is not callable.', join('.', array_merge([$varName], $functions)))); + $object = \call_user_func([$object, $fullFunctionName]); + } else { + throw new \RuntimeException(sprintf('"%s" is not callable.', implode('.', array_merge([$varName], $functions)))); } } - # End of the chain: call the method + // End of the chain: call the method else { - - if (is_callable(array($object, $fullFunctionName = 'get' . $function)) - || is_callable(array($object, $fullFunctionName = 'has' . $function)) - || is_callable(array($object, $fullFunctionName = 'is' . $function)) + if (\is_callable([$object, $fullFunctionName = 'get'.$function]) + || \is_callable([$object, $fullFunctionName = 'has'.$function]) + || \is_callable([$object, $fullFunctionName = 'is'.$function]) ) { - $objectValue = call_user_func_array(array($object, $fullFunctionName), $parameters); - } - else { - throw new \RuntimeException(sprintf('"%s" is not callable.', join('.', array_merge([$varName], $functions)))); + $objectValue = \call_user_func_array([$object, $fullFunctionName], $parameters); + } else { + throw new \RuntimeException(sprintf('"%s" is not callable.', implode('.', array_merge([$varName], $functions)))); } } } } - $routeParameter = str_replace($match[0][0], $objectValue, $value); + $routeParameter = str_replace($match[0][0], $objectValue, $value); $routeParameters[$key] = $routeParameter; } - - } } elseif (preg_match('#^\{(?P\w+)\}$#', $value, $matches)) { $routeParameters[$key] = $request->get($matches['parameter']); @@ -200,7 +189,7 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters = } $url = null; - if ( !is_null($routeName) ) { + if (null !== $routeName) { $referenceType = $routeAbsolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::RELATIVE_PATH; $url = $this->router->generate($routeName, $routeParameters, $referenceType); } @@ -208,11 +197,11 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters = $breadcrumb = new Breadcrumb($breadcrumb_or_title, $url, $attributes); } - if (!is_int($position)) { + if (!\is_int($position)) { throw new \InvalidArgumentException('The position of a breadcrumb must be an integer.'); } - if ($position == 0 || $position > $this->breadcrumbs->count()) { + if (0 == $position || $position > $this->breadcrumbs->count()) { $this->breadcrumbs->attach($breadcrumb); } else { $this->insert($breadcrumb, $position); @@ -226,7 +215,7 @@ private function insert($breadcrumb, $position) if ($position < 0) { $position += $this->breadcrumbs->count(); } else { // $position >= 1 - $position--; + --$position; } $breadcrumbs = new \SplObjectStorage(); @@ -245,7 +234,7 @@ private function insert($breadcrumb, $position) } /** - * Reset the trail + * Reset the trail. * * @return self */ diff --git a/src/DependencyInjection/APYBreadcrumbTrailExtension.php b/src/DependencyInjection/APYBreadcrumbTrailExtension.php index 01f18d0..61c72c4 100644 --- a/src/DependencyInjection/APYBreadcrumbTrailExtension.php +++ b/src/DependencyInjection/APYBreadcrumbTrailExtension.php @@ -29,17 +29,18 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('apy_breadcrumb_trail.template', $config['template']); - $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.xml'); - $this->deprecateService($container, "apy_breadcrumb_trail"); - $this->deprecateService($container, "apy_breadcrumb_trail.annotation.listener"); + $this->deprecateService($container, 'apy_breadcrumb_trail'); + $this->deprecateService($container, 'apy_breadcrumb_trail.annotation.listener'); } - private function deprecateService(ContainerBuilder $container, string $id) { + private function deprecateService(ContainerBuilder $container, string $id) + { $alias = $container->getAlias($id); if (Kernel::VERSION_ID >= 50100) { - $alias->setDeprecated('APY/BreadcrumbTrailBundle','1.7','The service is deprecated, use "%alias_id%" FQCN as service id instead.'); + $alias->setDeprecated('APY/BreadcrumbTrailBundle', '1.7', 'The service is deprecated, use "%alias_id%" FQCN as service id instead.'); } elseif (Kernel::VERSION_ID >= 40300) { $alias->setDeprecated(); } diff --git a/src/EventListener/BreadcrumbListener.php b/src/EventListener/BreadcrumbListener.php index 7bad2ae..a2649ef 100644 --- a/src/EventListener/BreadcrumbListener.php +++ b/src/EventListener/BreadcrumbListener.php @@ -11,10 +11,10 @@ namespace APY\BreadcrumbTrailBundle\EventListener; +use APY\BreadcrumbTrailBundle\Annotation\Breadcrumb; +use APY\BreadcrumbTrailBundle\BreadcrumbTrail\Trail; use APY\BreadcrumbTrailBundle\MixedAnnotationWithAttributeBreadcrumbsException; use Doctrine\Common\Annotations\Reader; -use APY\BreadcrumbTrailBundle\BreadcrumbTrail\Trail; -use APY\BreadcrumbTrailBundle\Annotation\Breadcrumb; use Symfony\Component\HttpKernel\Event\KernelEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -26,7 +26,6 @@ class BreadcrumbListener protected $reader; /** - * * @var Trail An Trail instance */ protected $breadcrumbTrail; @@ -34,8 +33,8 @@ class BreadcrumbListener /** * Constructor. * - * @param Reader $reader An Reader instance - * @param Trail $breadcrumbTrail An Trail instance + * @param Reader $reader An Reader instance + * @param Trail $breadcrumbTrail An Trail instance */ public function __construct(Reader $reader, Trail $breadcrumbTrail) { @@ -44,11 +43,11 @@ public function __construct(Reader $reader, Trail $breadcrumbTrail) } /** - * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent|\Symfony\Component\HttpKernel\Event\ControllerEvent $event + * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent|\Symfony\Component\HttpKernel\Event\ControllerEvent $event */ public function onKernelController(KernelEvent $event) { - if (!is_array($controller = $event->getController())) { + if (!\is_array($controller = $event->getController())) { return; } @@ -64,18 +63,18 @@ public function onKernelController(KernelEvent $event) throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class)); } - if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { + if (HttpKernelInterface::MASTER_REQUEST == $event->getRequestType()) { $this->breadcrumbTrail->reset(); // Annotations from class $classBreadcrumbs = $this->reader->getClassAnnotations($class); if ($this->supportsLoadingAttributes()) { $classAttributeBreadcrumbs = $this->getAttributes($class); - if (count($classBreadcrumbs) > 0) { + if (\count($classBreadcrumbs) > 0) { trigger_deprecation('apy/breadcrumb-bundle', '1.7', 'Please replace the annotations in "%s" with attributes. Adding Breadcrumbs via annotations is deprecated and will be removed in v2.0, but luckily your platform supports using Attributes.', $class->name); } - if (count($classAttributeBreadcrumbs) > 0) { - if (count($classBreadcrumbs) > 0) { + if (\count($classAttributeBreadcrumbs) > 0) { + if (\count($classBreadcrumbs) > 0) { throw MixedAnnotationWithAttributeBreadcrumbsException::forClass($class->name); } $classBreadcrumbs = $classAttributeBreadcrumbs; @@ -88,11 +87,11 @@ public function onKernelController(KernelEvent $event) $methodBreadcrumbs = $this->reader->getMethodAnnotations($method); if ($this->supportsLoadingAttributes()) { $methodAttributeBreadcrumbs = $this->getAttributes($method); - if (count($methodBreadcrumbs) > 0) { + if (\count($methodBreadcrumbs) > 0) { trigger_deprecation('apy/breadcrumb-bundle', '1.7', 'Please replace the annotations in "%s" with attributes. Adding Breadcrumbs via annotations is deprecated and will be removed in v2.0, but luckily your platform supports using Attributes.', $class->name.'::'.$method->name); } - if (count($methodAttributeBreadcrumbs) > 0) { - if (count($methodBreadcrumbs) > 0) { + if (\count($methodAttributeBreadcrumbs) > 0) { + if (\count($methodBreadcrumbs) > 0) { throw MixedAnnotationWithAttributeBreadcrumbsException::forClassMethod($class->name, $method->name); } $methodBreadcrumbs = $methodAttributeBreadcrumbs; @@ -113,9 +112,9 @@ private function addBreadcrumbsToTrail(array $annotations) $template = $annotation->getTemplate(); $title = $annotation->getTitle(); - if ($template != null) { + if (null != $template) { $this->breadcrumbTrail->setTemplate($template); - if ($title === null) { + if (null === $title) { continue; } } @@ -148,11 +147,12 @@ private function supportsLoadingAttributes(): bool /** * @param \ReflectionClass|\ReflectionMethod $reflected + * * @return array */ private function getAttributes($reflected): array { - if ($this->supportsLoadingAttributes() === false) { + if (false === $this->supportsLoadingAttributes()) { throw new \RuntimeException('Detected an attempt on getting attributes while your version of PHP does not support this.'); } diff --git a/src/MixedAnnotationWithAttributeBreadcrumbsException.php b/src/MixedAnnotationWithAttributeBreadcrumbsException.php index 35863ea..671718c 100644 --- a/src/MixedAnnotationWithAttributeBreadcrumbsException.php +++ b/src/MixedAnnotationWithAttributeBreadcrumbsException.php @@ -2,7 +2,6 @@ namespace APY\BreadcrumbTrailBundle; - class MixedAnnotationWithAttributeBreadcrumbsException extends \LogicException { private function __construct(string $class, string $method = null) @@ -11,7 +10,7 @@ private function __construct(string $class, string $method = null) if ($method) { $target .= "::$method()"; } else { - $target .= "::class"; + $target .= '::class'; } parent::__construct("A mix of Breadcrumb attributes and annotations is not allowed. Please upgrade all annotations on class `{$target}` to attributes."); diff --git a/src/Twig/BreadcrumbTrailExtension.php b/src/Twig/BreadcrumbTrailExtension.php index 7018f0a..0ccdda3 100644 --- a/src/Twig/BreadcrumbTrailExtension.php +++ b/src/Twig/BreadcrumbTrailExtension.php @@ -17,18 +17,15 @@ use Twig\TwigFunction; /** - * Provides an extension for Twig to output breadcrumbs + * Provides an extension for Twig to output breadcrumbs. */ class BreadcrumbTrailExtension extends AbstractExtension { private $trail; private $templating; - /** * BreadcrumbTrailExtension constructor. - * @param Trail $trail - * @param Environment $templating */ public function __construct(Trail $trail, Environment $templating) { @@ -43,21 +40,21 @@ public function __construct(Trail $trail, Environment $templating) */ public function getFunctions() { - return array( - new TwigFunction("apy_breadcrumb_trail_render", array($this, "renderBreadcrumbTrail"), array("is_safe" => array("html"))), - ); + return [ + new TwigFunction('apy_breadcrumb_trail_render', [$this, 'renderBreadcrumbTrail'], ['is_safe' => ['html']]), + ]; } /** - * Renders the breadcrumb trail in a list + * Renders the breadcrumb trail in a list. * * @return string */ public function renderBreadcrumbTrail($template = null) { return $this->templating->render( - $template === null ? $this->trail->getTemplate() : $template, - array( 'breadcrumbs' => $this->trail ) + null === $template ? $this->trail->getTemplate() : $template, + ['breadcrumbs' => $this->trail] ); } @@ -68,6 +65,6 @@ public function renderBreadcrumbTrail($template = null) */ public function getName() { - return "breadcrumbtrail"; + return 'breadcrumbtrail'; } } diff --git a/tests/Annotation/BreadcrumbTest.php b/tests/Annotation/BreadcrumbTest.php index 68cb84b..9c09141 100644 --- a/tests/Annotation/BreadcrumbTest.php +++ b/tests/Annotation/BreadcrumbTest.php @@ -8,7 +8,7 @@ class BreadcrumbTest extends TestCase { public function testConstructWithSimpleTitle() { - $expected = "title-of-the-breadcrumb"; + $expected = 'title-of-the-breadcrumb'; $breadcrumb = new Breadcrumb($expected); self::assertEquals($expected, $breadcrumb->getTitle()); @@ -16,7 +16,7 @@ public function testConstructWithSimpleTitle() public function testConstructWithLegacyArray() { - $expected = "title-of-the-breadcrumb"; + $expected = 'title-of-the-breadcrumb'; $breadcrumb = new Breadcrumb(['title' => $expected]); self::assertEquals($expected, $breadcrumb->getTitle()); @@ -24,11 +24,11 @@ public function testConstructWithLegacyArray() public function testConstructWithLegacyRouteArray() { - $expected = "title-of-the-breadcrumb"; + $expected = 'title-of-the-breadcrumb'; $expectedRouteAbsolute = true; $breadcrumb = new Breadcrumb([ 'title' => $expected, - 'route' => ['absolute' => $expectedRouteAbsolute] + 'route' => ['absolute' => $expectedRouteAbsolute], ]); self::assertEquals($expected, $breadcrumb->getTitle()); @@ -37,7 +37,7 @@ public function testConstructWithLegacyRouteArray() public function testValueWillGetInterpretedAsTitle() { - $expected = "title-of-the-breadcrumb"; + $expected = 'title-of-the-breadcrumb'; $breadcrumb = new Breadcrumb(['value' => $expected]); self::assertEquals($expected, $breadcrumb->getTitle()); @@ -45,8 +45,8 @@ public function testValueWillGetInterpretedAsTitle() public function testUnnamedWillGetInterpretedAsValueForTitle() { - $expected = "title-of-the-breadcrumb"; - $breadcrumb = new Breadcrumb([$expected, 'route' => ["name" => "my_route"]]); + $expected = 'title-of-the-breadcrumb'; + $breadcrumb = new Breadcrumb([$expected, 'route' => ['name' => 'my_route']]); self::assertEquals($expected, $breadcrumb->getTitle()); } diff --git a/tests/EventListener/BreadcrumbListenerTest.php b/tests/EventListener/BreadcrumbListenerTest.php index b9262eb..1f1a9c6 100644 --- a/tests/EventListener/BreadcrumbListenerTest.php +++ b/tests/EventListener/BreadcrumbListenerTest.php @@ -87,6 +87,7 @@ private function createControllerEvent($controller) if (Kernel::MAJOR_VERSION <= 4) { return new FilterControllerEvent($this->kernel, [$controller, 'indexAction'], new Request(), HttpKernelInterface::MASTER_REQUEST); } + return new ControllerEvent($this->kernel, [$controller, 'indexAction'], new Request(), HttpKernelInterface::MASTER_REQUEST); } } diff --git a/tests/ExtensionTest.php b/tests/ExtensionTest.php index d6281f8..d20bf86 100644 --- a/tests/ExtensionTest.php +++ b/tests/ExtensionTest.php @@ -13,7 +13,7 @@ class ExtensionTest extends AbstractExtensionTestCase protected function getContainerExtensions(): array { return [ - new APYBreadcrumbTrailExtension() + new APYBreadcrumbTrailExtension(), ]; }