Skip to content

Commit

Permalink
cs fix (#308)
Browse files Browse the repository at this point in the history
this is a bunch of fix in coding standard, extracted from previous
PR for new v3 release. It includes:

* explicit root functions
* fix int/bool names (e.g. instead of integer/boolean)
* fix some phpdoc missing lines
* fix some non-yoda comparisons
* sort imports
* add some forgotten type hints/return types
* remove some irrelevant spaces
* fix some phpdoc wrong names

Co-Authored-By: Alexander M. Turek <[email protected]>
  • Loading branch information
garak and derrabus authored Aug 25, 2019
1 parent 4e32187 commit 13e3e32
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/Knp/Menu/Factory/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CoreExtension implements ExtensionInterface
*/
public function buildOptions(array $options): array
{
return array_merge(
return \array_merge(
[
'uri' => null,
'label' => null,
Expand Down
1 change: 1 addition & 0 deletions src/Knp/Menu/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function setDisplay(bool $bool);
* @param array $options If creating a new item, the options passed to the factory for the item
*
* @return ItemInterface
*
* @throws \InvalidArgumentException if the item is already in a tree
*/
public function addChild(self $child, array $options = []): self;
Expand Down
4 changes: 2 additions & 2 deletions src/Knp/Menu/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function __construct(FactoryInterface $factory)
public function load($data): ItemInterface
{
if (!$this->supports($data)) {
throw new \InvalidArgumentException(sprintf('Unsupported data. Expected an array but got ', is_object($data) ? get_class($data) : gettype($data)));
throw new \InvalidArgumentException(\sprintf('Unsupported data. Expected an array but got %s', \is_object($data) ? \get_class($data) : \gettype($data)));
}

return $this->fromArray($data);
}

public function supports($data): bool
{
return is_array($data);
return \is_array($data);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Knp/Menu/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function load($data): ItemInterface;
*
* @param mixed $data
*
* @return boolean
* @return bool
*/
public function supports($data): bool;
}
7 changes: 4 additions & 3 deletions src/Knp/Menu/Loader/NodeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knp\Menu\Loader;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Knp\Menu\NodeInterface;

class NodeLoader implements LoaderInterface
Expand All @@ -14,10 +15,10 @@ public function __construct(FactoryInterface $factory)
$this->factory = $factory;
}

public function load($data)
public function load($data): ItemInterface
{
if (!$data instanceof NodeInterface) {
throw new \InvalidArgumentException(sprintf('Unsupported data. Expected Knp\Menu\NodeInterface but got %s', is_object($data) ? get_class($data) : gettype($data)));
throw new \InvalidArgumentException(\sprintf('Unsupported data. Expected Knp\Menu\NodeInterface but got %s', \is_object($data) ? \get_class($data) : \gettype($data)));
}

$item = $this->factory->createItem($data->getName(), $data->getOptions());
Expand All @@ -29,7 +30,7 @@ public function load($data)
return $item;
}

public function supports($data)
public function supports($data): bool
{
return $data instanceof NodeInterface;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Knp/Menu/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function isCurrent(ItemInterface $item): bool
}
}

$current = (boolean) $current;
$current = (bool) $current;
$this->cache[$item] = $current;

return $current;
Expand Down
2 changes: 1 addition & 1 deletion src/Knp/Menu/Matcher/MatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface MatcherInterface
*
* @param ItemInterface $item
*
* @return boolean
* @return bool
*/
public function isCurrent(ItemInterface $item): bool;

Expand Down
6 changes: 3 additions & 3 deletions src/Knp/Menu/Matcher/Voter/RouteVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function matchItem(ItemInterface $item): ?bool
$routes = (array) $item->getExtra('routes', []);

foreach ($routes as $testedRoute) {
if (is_string($testedRoute)) {
if (\is_string($testedRoute)) {
$testedRoute = ['route' => $testedRoute];
}

if (!is_array($testedRoute)) {
if (!\is_array($testedRoute)) {
throw new \InvalidArgumentException('Routes extra items must be strings or arrays.');
}

Expand All @@ -67,7 +67,7 @@ private function isMatchingRoute(Request $request, array $testedRoute): bool
return false;
}
} elseif (!empty($testedRoute['pattern'])) {
if (!preg_match($testedRoute['pattern'], $route)) {
if (!\preg_match($testedRoute['pattern'], $route)) {
return false;
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/Knp/Menu/MenuFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MenuFactory implements FactoryInterface
private $extensions = [];

/**
* @var ExtensionInterface[]
* @var ExtensionInterface[]|null
*/
private $sorted;

Expand Down Expand Up @@ -55,13 +55,13 @@ public function addExtension(ExtensionInterface $extension, int $priority = 0):
/**
* Sorts the internal list of extensions by priority.
*
* @return ExtensionInterface[]
* @return ExtensionInterface[]|null
*/
private function getExtensions(): ?array
{
if (null === $this->sorted) {
krsort($this->extensions);
$this->sorted = !empty($this->extensions) ? call_user_func_array('array_merge', $this->extensions) : [];
\krsort($this->extensions);
$this->sorted = !empty($this->extensions) ? \call_user_func_array('array_merge', $this->extensions) : [];
}

return $this->sorted;
Expand Down
25 changes: 19 additions & 6 deletions src/Knp/Menu/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,91 @@ class MenuItem implements ItemInterface
{
/**
* Name of this menu item (used for id by parent menu)
*
* @var string
*/
protected $name;

/**
* Label to output, name is used by default
*
* @var string|null
*/
protected $label;

/**
* Attributes for the item link
*
* @var array
*/
protected $linkAttributes = [];

/**
* Attributes for the children list
*
* @var array
*/
protected $childrenAttributes = [];

/**
* Attributes for the item text
*
* @var array
*/
protected $labelAttributes = [];

/**
* Uri to use in the anchor tag
*
* @var string|null
*/
protected $uri;

/**
* Attributes for the item
*
* @var array
*/
protected $attributes = [];

/**
* Extra stuff associated to the item
*
* @var array
*/
protected $extras = [];

/**
* Whether the item is displayed
*
* @var bool
*/
protected $display = true;

/**
* Whether the children of the item are displayed
*
* @var bool
*/
protected $displayChildren = true;

/**
* Child items
*
* @var ItemInterface[]
*/
protected $children = [];

/**
* Parent item
*
* @var ItemInterface|null
*/
protected $parent;

/**
* whether the item is current. null means unknown
*
* @var bool|null
*/
protected $isCurrent;
Expand Down Expand Up @@ -137,13 +150,13 @@ public function setName(string $name): ItemInterface
$this->name = $name;

if (null !== $parent) {
$names = array_keys($parent->getChildren());
$items = array_values($parent->getChildren());
$names = \array_keys($parent->getChildren());
$items = \array_values($parent->getChildren());

$offset = array_search($oldName, $names);
$offset = \array_search($oldName, $names);
$names[$offset] = $name;

$parent->setChildren(array_combine($names, $items));
$parent->setChildren(\array_combine($names, $items));
}

return $this;
Expand All @@ -163,7 +176,7 @@ public function setUri(?string $uri): ItemInterface

public function getLabel(): string
{
return ($this->label !== null) ? $this->label : $this->name;
return (null !== $this->label) ? $this->label : $this->name;
}

public function setLabel(?string $label): ItemInterface
Expand Down Expand Up @@ -550,7 +563,7 @@ public function actsLikeLast(): bool
return true;
}

$children = array_reverse($this->getParent()->getChildren());
$children = \array_reverse($this->getParent()->getChildren());
foreach ($children as $child) {
// loop until we find a visible menu. If its this menu, we're first
if ($child->isDisplayed()) {
Expand Down
6 changes: 3 additions & 3 deletions src/Knp/Menu/Provider/ArrayAccessProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function __construct(\ArrayAccess $registry, array $menuIds = [])
public function get(string $name, array $options = []): ItemInterface
{
if (!isset($this->menuIds[$name])) {
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}

$menu = $this->registry[$this->menuIds[$name]];

if (is_callable($menu)) {
$menu = call_user_func($menu, $options, $this->registry);
if (\is_callable($menu)) {
$menu = \call_user_func($menu, $options, $this->registry);
}

return $menu;
Expand Down
2 changes: 1 addition & 1 deletion src/Knp/Menu/Provider/ChainProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function get(string $name, array $options = []): ItemInterface
}
}

throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}

public function has(string $name, array $options = []): bool
Expand Down
10 changes: 5 additions & 5 deletions src/Knp/Menu/Provider/LazyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ public function __construct(array $builders)
public function get(string $name, array $options = []): ItemInterface
{
if (!isset($this->builders[$name])) {
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}

$builder = $this->builders[$name];

if (is_array($builder) && isset($builder[0]) && $builder[0] instanceof \Closure) {
if (\is_array($builder) && isset($builder[0]) && $builder[0] instanceof \Closure) {
$builder[0] = $builder[0]();
}

if (!is_callable($builder)) {
throw new \LogicException(sprintf('Invalid menu builder for "%s". A callable or a factory for an object callable are expected.', $name));
if (!\is_callable($builder)) {
throw new \LogicException(\sprintf('Invalid menu builder for "%s". A callable or a factory for an object callable are expected.', $name));
}

return call_user_func($builder, $options);
return $builder($options);
}

public function has($name, array $options = []): bool
Expand Down
3 changes: 2 additions & 1 deletion src/Knp/Menu/Provider/MenuProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface MenuProviderInterface
* @param array $options
*
* @return ItemInterface
*
* @throws \InvalidArgumentException if the menu does not exists
*/
public function get(string $name, array $options = []): ItemInterface;
Expand All @@ -23,7 +24,7 @@ public function get(string $name, array $options = []): ItemInterface;
* @param string $name
* @param array $options
*
* @return boolean
* @return bool
*/
public function has(string $name, array $options = []): bool;
}
2 changes: 1 addition & 1 deletion src/Knp/Menu/Provider/PsrProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(ContainerInterface $container)
public function get(string $name, array $options = []): ItemInterface
{
if (!$this->container->has($name)) {
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name));
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}

return $this->container->get($name);
Expand Down
2 changes: 1 addition & 1 deletion src/Knp/Menu/Renderer/ArrayAccessProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function get(string $name = null): RendererInterface
}

if (!isset($this->rendererIds[$name])) {
throw new \InvalidArgumentException(sprintf('The renderer "%s" is not defined.', $name));
throw new \InvalidArgumentException(\sprintf('The renderer "%s" is not defined.', $name));
}

return $this->registry[$this->rendererIds[$name]];
Expand Down
Loading

0 comments on commit 13e3e32

Please sign in to comment.