Skip to content

Commit

Permalink
Add some return type hints [3.x] (#24)
Browse files Browse the repository at this point in the history
Some of the type hints resolve deprecation notices emitted by Symfony, to prepare for adding type hints in future Symfony releases.
  • Loading branch information
mpdude authored May 5, 2022
1 parent 3858c53 commit fc467c7
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 127 deletions.
8 changes: 2 additions & 6 deletions Build/BuildContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ public function __construct(array $params)

/**
* @param array<string, mixed> $params
*
* @return BuildContext
*/
public function change(array $params)
public function change(array $params): self
{
return new self(array_merge($this->params, $params));
}

/**
* @param string $name
*
* @return mixed|null
*/
public function get($name)
public function get(string $name)
{
return isset($this->params[$name]) ? $this->params[$name] : null;
}
Expand Down
3 changes: 3 additions & 0 deletions Build/BuildDirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
*/
interface BuildDirector
{
/**
* @return void
*/
public function build(BuildContext $c, Tree $t, BuildDispatcher $d);
}
12 changes: 6 additions & 6 deletions Build/BuildDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class BuildDispatcher
protected $resources = [];
protected $queue;

public function addDirector(BuildDirector $director, $priority = 100)
public function addDirector(BuildDirector $director, $priority = 100): void
{
$this->directors[$priority][] = $director;
}

public function start(Tree $tree)
public function start(Tree $tree): void
{
$directorsOrderedByPriority = $this->getDirectorsOrderedByPriority();
$this->queue = [new BuildContext([])];
Expand All @@ -33,25 +33,25 @@ public function start(Tree $tree)
}
}

public function search(BuildContext $context)
public function search(BuildContext $context): void
{
$this->queue[] = $context;
}

public function addResource(ResourceInterface $resource)
public function addResource(ResourceInterface $resource): void
{
$this->resources[] = $resource;
}

public function getResources()
public function getResources(): array
{
return array_unique($this->resources);
}

/**
* @return BuildDirector[]
*/
private function getDirectorsOrderedByPriority()
private function getDirectorsOrderedByPriority(): array
{
krsort($this->directors, SORT_NUMERIC);
$buildDirectorsOrderedByPriority = [];
Expand Down
20 changes: 6 additions & 14 deletions Build/TreeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TreeFactory implements ServiceSubscriberInterface
/** @var ContainerInterface */
protected $container;

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [
BuildDispatcher::class
Expand All @@ -64,19 +64,14 @@ public function __construct(
$this->stopwatch = $stopwatch;
}

public function debug($msg)
public function debug(string $msg): void
{
if ($this->logger) {
$this->logger->debug("$msg (PID ".getmypid().', microtime '.microtime().')');
}
}

/**
* @param $sectionName
*
* @return StopwatchEvent|null
*/
protected function startTiming($sectionName)
protected function startTiming(string $sectionName): ?StopwatchEvent
{
if ($this->stopwatch) {
return $this->stopwatch->start('webfactory/navigation-bundle: '.$sectionName);
Expand All @@ -85,17 +80,14 @@ protected function startTiming($sectionName)
return null;
}

protected function stopTiming(StopwatchEvent $watch = null)
protected function stopTiming(StopwatchEvent $watch = null): void
{
if ($watch) {
$watch->stop();
}
}

/**
* @return Tree
*/
public function getTree()
public function getTree(): Tree
{
if (!$this->_tree) {
$self = $this;
Expand Down Expand Up @@ -124,7 +116,7 @@ public function getTree()
return $this->_tree;
}

public function buildTreeCache(ConfigCacheInterface $cache)
public function buildTreeCache(ConfigCacheInterface $cache): void
{
$this->_tree = new Tree();
// Dynamic (runtime) lookup:
Expand Down
2 changes: 1 addition & 1 deletion Command/DumpTreeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function dumpNode(Node $n, OutputInterface $output, $depth = 0)
private function dumpNode(Node $n, OutputInterface $output, $depth = 0): void
{
$first = true;

Expand Down
9 changes: 5 additions & 4 deletions Controller/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class NavigationController extends AbstractController
{
public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return array_merge(parent::getSubscribedServices(), [
Tree::class,
Expand All @@ -39,7 +39,8 @@ public function treeAction(
$maxLevels = 1,
$expandedLevels = 1,
$template = '@WebfactoryNavigation/Navigation/navigation.html.twig'
) {
): Response
{
if (\is_array($root)) {
$node = $this->getTree()->find($root);
if (!$node) {
Expand Down Expand Up @@ -81,7 +82,7 @@ public function treeAction(
*
* @return Response
*/
public function ancestryAction($startLevel, $maxLevels = 1, $expandedLevels = 1, $template = '@WebfactoryNavigation/Navigation/navigation.html.twig')
public function ancestryAction($startLevel, $maxLevels = 1, $expandedLevels = 1, $template = '@WebfactoryNavigation/Navigation/navigation.html.twig'): Response
{
if ($node = $this->getTree()->getActivePath()) {
$path = $node->getPath();
Expand All @@ -101,7 +102,7 @@ public function ancestryAction($startLevel, $maxLevels = 1, $expandedLevels = 1,
*
* @return Response
*/
public function breadcrumbsAction($template = '@WebfactoryNavigation/Navigation/breadcrumbs.html.twig')
public function breadcrumbsAction($template = '@WebfactoryNavigation/Navigation/breadcrumbs.html.twig'): Response
{
if ($node = $this->getTree()->getActivePath()) {
return $this->render($template, [
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/BuildDirectorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class BuildDirectorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (false === $container->hasDefinition(BuildDispatcher::class)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/WebfactoryNavigationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class WebfactoryNavigationExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
2 changes: 1 addition & 1 deletion Event/ActiveNodeEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(RequestStack $requestStack)
$this->requestStack = $requestStack;
}

public function initializeTree(TreeInitializedEvent $event)
public function initializeTree(TreeInitializedEvent $event): void
{
if ($masterRequest = $this->requestStack->getMasterRequest()) {
if ($node = $event->getTree()->find($masterRequest->attributes->all())) {
Expand Down
5 changes: 1 addition & 4 deletions Event/TreeInitializedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public function __construct(Tree $tree)
$this->tree = $tree;
}

/**
* @return Tree
*/
public function getTree()
public function getTree(): Tree
{
return $this->tree;
}
Expand Down
10 changes: 2 additions & 8 deletions Tree/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ class Finder
/** @var string[] */
protected $idToHash = [];

/**
* @param Node $object
* @param array $requirements
*/
public function add($object, array $requirements)
public function add(Node $object, array $requirements): void
{
$hash = spl_object_hash($object);
$this->objects[$hash] = $object;
Expand All @@ -48,10 +44,8 @@ public function add($object, array $requirements)

/**
* @param string|array|object $provided
*
* @return Node|null
*/
public function lookup($provided)
public function lookup($provided): ?Node
{
$remainingCount = [];
$maxMatch = 0;
Expand Down
Loading

0 comments on commit fc467c7

Please sign in to comment.