Skip to content

Commit

Permalink
minor #32238 [DependencyInjection] Added type-hints on compiler passe…
Browse files Browse the repository at this point in the history
…s (derrabus)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[DependencyInjection] Added type-hints on compiler passes

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32179
| License       | MIT
| Doc PR        | N/A

First part of type-hints for the DependencyInjection component. I've started with the compiler passes and since there's quite a lot of them, I've decided to split them off.

Commits
-------

d5bc28a [DependencyInjection] Added type-hints on compiler passes.
  • Loading branch information
fabpot committed Jun 29, 2019
2 parents 7739849 + d5bc28a commit faaa83c
Show file tree
Hide file tree
Showing 28 changed files with 37 additions and 54 deletions.
Expand Up @@ -68,11 +68,10 @@ protected function inExpression(bool $reset = true): bool
* Processes a value found in a definition tree.
*
* @param mixed $value
* @param bool $isRoot
*
* @return mixed The processed value
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (\is_array($value)) {
foreach ($value as $k => $v) {
Expand Down Expand Up @@ -105,14 +104,11 @@ protected function processValue($value, $isRoot = false)
}

/**
* @param Definition $definition
* @param bool $required
*
* @return \ReflectionFunctionAbstract|null
*
* @throws RuntimeException
*/
protected function getConstructor(Definition $definition, $required)
protected function getConstructor(Definition $definition, bool $required)
{
if (\is_string($factory = $definition->getFactory())) {
if (!\function_exists($factory)) {
Expand Down Expand Up @@ -161,14 +157,11 @@ protected function getConstructor(Definition $definition, $required)
}

/**
* @param Definition $definition
* @param string $method
*
* @throws RuntimeException
*
* @return \ReflectionFunctionAbstract
*/
protected function getReflectionMethod(Definition $definition, $method)
protected function getReflectionMethod(Definition $definition, string $method)
{
if ('__construct' === $method) {
return $this->getConstructor($definition, true);
Expand Down
Expand Up @@ -74,7 +74,7 @@ public function process(ContainerBuilder $container)
}
}

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
$lazy = $this->lazy;
$inExpression = $this->inExpression();
Expand Down
Expand Up @@ -65,7 +65,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
try {
return $this->doProcessValue($value, $isRoot);
Expand All @@ -80,7 +80,7 @@ protected function processValue($value, $isRoot = false)
}
}

private function doProcessValue($value, $isRoot = false)
private function doProcessValue($value, bool $isRoot = false)
{
if ($value instanceof TypedReference) {
if ($ref = $this->getAutowiredReference($value)) {
Expand Down Expand Up @@ -373,7 +373,7 @@ private function set(string $type, string $id)
$this->ambiguousServiceTypes[$type][] = $id;
}

private function createTypeNotFoundMessageCallback(TypedReference $reference, $label)
private function createTypeNotFoundMessageCallback(TypedReference $reference, string $label)
{
$container = new ContainerBuilder($this->container->getParameterBag());
$container->setAliases($this->container->getAliases());
Expand All @@ -386,7 +386,7 @@ private function createTypeNotFoundMessageCallback(TypedReference $reference, $l
};
}

private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label, string $currentId)
private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, string $label, string $currentId)
{
if (!$r = $container->getReflectionClass($type = $reference->getType(), false)) {
// either $type does not exist or a parent class does not exist
Expand Down Expand Up @@ -444,7 +444,7 @@ private function createTypeAlternatives(ContainerBuilder $container, TypedRefere
return sprintf(' You should maybe alias this %s to %s.', class_exists($type, false) ? 'class' : 'interface', $message);
}

private function getAliasesSuggestionForType(ContainerBuilder $container, $type, $extraContext = null)
private function getAliasesSuggestionForType(ContainerBuilder $container, string $type, $extraContext = null)
{
$aliases = [];
foreach (class_parents($type) + class_implements($type) as $parent) {
Expand Down
Expand Up @@ -23,7 +23,7 @@ class AutowireRequiredMethodsPass extends AbstractRecursivePass
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
$value = parent::processValue($value, $isRoot);

Expand Down
Expand Up @@ -32,7 +32,7 @@ public function __construct(bool $throwExceptions = true)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -43,7 +43,7 @@ public function process(ContainerBuilder $container)
}
}

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Reference) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -25,7 +25,7 @@
*/
class CheckReferenceValidityPass extends AbstractRecursivePass
{
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($isRoot && $value instanceof Definition && ($value->isSynthetic() || $value->isAbstract())) {
return $value;
Expand Down
Expand Up @@ -53,12 +53,8 @@ public function getServiceReferenceGraph()

/**
* Adds a pass to the PassConfig.
*
* @param CompilerPassInterface $pass A compiler pass
* @param string $type The type of the pass
* @param int $priority Used to sort the passes
*/
public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
public function addPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
{
$this->passConfig->addPass($pass, $type, $priority);
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ class DefinitionErrorExceptionPass extends AbstractRecursivePass
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition || !$value->hasErrors()) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -101,7 +101,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof ArgumentInterface) {
// Reference found in ArgumentInterface::getValues() are not inlineable
Expand Down Expand Up @@ -155,7 +155,7 @@ protected function processValue($value, $isRoot = false)
*
* @return bool If the definition is inlineable
*/
private function isInlineableDefinition($id, Definition $definition)
private function isInlineableDefinition(string $id, Definition $definition)
{
if ($definition->hasErrors() || $definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
return false;
Expand Down
Expand Up @@ -110,13 +110,9 @@ public function getPasses()
/**
* Adds a pass.
*
* @param CompilerPassInterface $pass A Compiler pass
* @param string $type The pass type
* @param int $priority Used to sort the passes
*
* @throws InvalidArgumentException when a pass type doesn't exist
*/
public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
public function addPass(CompilerPassInterface $pass, string $type = self::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
{
$property = $type.'Passes';
if (!isset($this->$property)) {
Expand Down
Expand Up @@ -60,7 +60,7 @@ public function process(ContainerBuilder $container)
}
}

private static function validateProvidedTypes($types, $class)
private static function validateProvidedTypes(string $types, string $class)
{
$types = explode('|', $types);

Expand Down
Expand Up @@ -25,7 +25,7 @@
*/
class RegisterServiceSubscribersPass extends AbstractRecursivePass
{
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('container.service_subscriber')) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -75,7 +75,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Reference) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -80,7 +80,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof Reference && isset($this->replacements[$referenceId = (string) $value])) {
// Perform the replacement
Expand Down
Expand Up @@ -90,7 +90,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof TypedReference && $value->getType() === (string) $value) {
// Already checked
Expand Down
Expand Up @@ -28,7 +28,7 @@ class ResolveChildDefinitionsPass extends AbstractRecursivePass
{
private $currentPath;

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -18,7 +18,7 @@
*/
class ResolveEnvPlaceholdersPass extends AbstractRecursivePass
{
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (\is_string($value)) {
return $this->container->resolveEnvPlaceholders($value, true);
Expand Down
Expand Up @@ -22,7 +22,7 @@ class ResolveFactoryClassPass extends AbstractRecursivePass
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof Definition && \is_array($factory = $value->getFactory()) && null === $factory[0]) {
if (null === $class = $value->getClass()) {
Expand Down
Expand Up @@ -47,7 +47,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof ArgumentInterface) {
return $value;
Expand Down
Expand Up @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container)
}
}

private function processDefinition(ContainerBuilder $container, $id, Definition $definition)
private function processDefinition(ContainerBuilder $container, string $id, Definition $definition)
{
$instanceofConditionals = $definition->getInstanceofConditionals();
$autoconfiguredInstanceof = $definition->isAutoconfigured() ? $container->getAutoconfiguredInstanceof() : [];
Expand Down
Expand Up @@ -53,7 +53,7 @@ public function process(ContainerBuilder $container)
*
* @throws RuntimeException When an invalid reference is found
*/
private function processValue($value, $rootLevel = 0, $level = 0)
private function processValue($value, int $rootLevel = 0, int $level = 0)
{
if ($value instanceof ServiceClosureArgument) {
$value->setValues($this->processValue($value->getValues(), 1, 1));
Expand Down
Expand Up @@ -26,7 +26,7 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -58,7 +58,7 @@ public function process(ContainerBuilder $container)
$this->bag = null;
}

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (\is_string($value)) {
$v = $this->bag->resolveValue($value);
Expand Down
Expand Up @@ -42,7 +42,7 @@ public function process(ContainerBuilder $container)
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Reference) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -25,7 +25,7 @@ class ResolveServiceSubscribersPass extends AbstractRecursivePass
{
private $serviceLocator;

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof Reference && $this->serviceLocator && \in_array((string) $value, [ContainerInterface::class, ServiceProviderInterface::class], true)) {
return new Reference($this->serviceLocator);
Expand Down
Expand Up @@ -25,7 +25,7 @@ class ResolveTaggedIteratorArgumentPass extends AbstractRecursivePass
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof TaggedIteratorArgument) {
return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -29,7 +29,7 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass
{
use PriorityTaggedServiceTrait;

protected function processValue($value, $isRoot = false)
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof ServiceLocatorArgument) {
if ($value->getTaggedIteratorArgument()) {
Expand Down Expand Up @@ -87,11 +87,9 @@ protected function processValue($value, $isRoot = false)
}

/**
* @param ContainerBuilder $container
* @param Reference[] $refMap
* @param string|null $callerId
* @param Reference[] $refMap
*/
public static function register(ContainerBuilder $container, array $refMap, $callerId = null): Reference
public static function register(ContainerBuilder $container, array $refMap, string $callerId = null): Reference
{
foreach ($refMap as $id => $ref) {
if (!$ref instanceof Reference) {
Expand Down

0 comments on commit faaa83c

Please sign in to comment.