Skip to content

Commit

Permalink
minor #33266 [DependencyInjection] Add types to private/final/interna…
Browse files Browse the repository at this point in the history
…l methods and constructors (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[DependencyInjection] Add types to private/final/internal methods and constructors

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

Commits
-------

def0ac7 Add types to private/final/internal methods and constructors.
  • Loading branch information
nicolas-grekas committed Sep 8, 2019
2 parents afad962 + def0ac7 commit 200eae3
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Test;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -137,7 +138,7 @@ public function getRemovedIds(): array
return $this->getPublicContainer()->getRemovedIds();
}

private function getPublicContainer()
private function getPublicContainer(): Container
{
if (null === $container = $this->kernel->getContainer()) {
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
Expand All @@ -146,7 +147,7 @@ private function getPublicContainer()
return $container;
}

private function getPrivateContainer()
private function getPrivateContainer(): ContainerInterface
{
return $this->getPublicContainer()->get($this->privateServicesLocatorId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ protected function getReflectionMethod(Definition $definition, $method)
return $r;
}

private function getExpressionLanguage()
private function getExpressionLanguage(): ExpressionLanguage
{
if (null === $this->expressionLanguage) {
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}

$providers = $this->container->getExpressionLanguageProviders();
$this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
$this->expressionLanguage = new ExpressionLanguage(null, $providers, function (string $arg): string {
if ('""' === substr_replace($arg, '', 1, -1)) {
$id = stripcslashes(substr($arg, 1, -1));
$this->inExpression = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ protected function processValue($value, $isRoot = false)
}
}

/**
* @return mixed
*/
private function doProcessValue($value, bool $isRoot = false)
{
if ($value instanceof TypedReference) {
Expand Down Expand Up @@ -371,7 +374,7 @@ private function set(string $type, string $id)
$this->ambiguousServiceTypes[$type][] = $id;
}

private function createTypeNotFoundMessageCallback(TypedReference $reference, string $label)
private function createTypeNotFoundMessageCallback(TypedReference $reference, string $label): callable
{
if (null === $this->typesClone->container) {
$this->typesClone->container = new ContainerBuilder($this->container->getParameterBag());
Expand All @@ -386,7 +389,7 @@ private function createTypeNotFoundMessageCallback(TypedReference $reference, st
})->bindTo($this->typesClone);
}

private function createTypeNotFoundMessage(TypedReference $reference, string $label, string $currentId)
private function createTypeNotFoundMessage(TypedReference $reference, string $label, string $currentId): string
{
if (!$r = $this->container->getReflectionClass($type = $reference->getType(), false)) {
// either $type does not exist or a parent class does not exist
Expand Down Expand Up @@ -420,7 +423,7 @@ private function createTypeNotFoundMessage(TypedReference $reference, string $la
return $message;
}

private function createTypeAlternatives(ContainerBuilder $container, TypedReference $reference)
private function createTypeAlternatives(ContainerBuilder $container, TypedReference $reference): string
{
// try suggesting available aliases first
if ($message = $this->getAliasesSuggestionForType($container, $type = $reference->getType())) {
Expand All @@ -444,7 +447,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, string $type)
private function getAliasesSuggestionForType(ContainerBuilder $container, string $type): ?string
{
$aliases = [];
foreach (class_parents($type) + class_implements($type) as $parent) {
Expand Down
Original file line number Diff line number Diff line change
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): array
{
$types = explode('|', $types);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ protected function processValue($value, $isRoot = false)
return parent::processValue($value, $isRoot);
}

/**
* @return mixed
*/
private function getBindingValue(BoundArgument $binding)
{
list($bindingValue, $bindingId) = $binding->getValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ResolveHotPathPass extends AbstractRecursivePass
private $tagName;
private $resolvedIds = [];

public function __construct($tagName = 'container.hot_path')
public function __construct(string $tagName = 'container.hot_path')
{
$this->tagName = $tagName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container)
}
}

private function processDefinition(ContainerBuilder $container, string $id, Definition $definition)
private function processDefinition(ContainerBuilder $container, string $id, Definition $definition): Definition
{
$instanceofConditionals = $definition->getInstanceofConditionals();
$autoconfiguredInstanceof = $definition->isAutoconfigured() ? $container->getAutoconfiguredInstanceof() : [];
Expand Down Expand Up @@ -144,7 +144,7 @@ private function processDefinition(ContainerBuilder $container, string $id, Defi
return $definition;
}

private function mergeConditionals(array $autoconfiguredInstanceof, array $instanceofConditionals, ContainerBuilder $container)
private function mergeConditionals(array $autoconfiguredInstanceof, array $instanceofConditionals, ContainerBuilder $container): array
{
// make each value an array of ChildDefinition
$conditionals = array_map(function ($childDef) { return [$childDef]; }, $autoconfiguredInstanceof);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function process(ContainerBuilder $container)
/**
* Processes arguments to determine invalid references.
*
* @return mixed
*
* @throws RuntimeException When an invalid reference is found
*/
private function processValue($value, int $rootLevel = 0, int $level = 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ protected function processValue($value, $isRoot = false)

/**
* @param Reference[] $refMap
* @param string|null $callerId
*/
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
7 changes: 6 additions & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,14 @@ protected function getEnv($name)
}

/**
* @param string|false $registry
* @param string|bool $load
*
* @return mixed
*
* @internal
*/
final protected function getService($registry, $id, $method, $load)
final protected function getService($registry, string $id, ?string $method, $load)
{
if ('service_container' === $id) {
return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ private function doResolveServices($value, array &$inlineServices = [], bool $is

yield $k => $this->resolveServices($v);
}
}, function () use ($value) {
}, function () use ($value): int {
$count = 0;
foreach ($value->getValues() as $v) {
foreach (self::getServiceConditionals($v) as $s) {
Expand Down Expand Up @@ -1641,7 +1641,7 @@ private function shareService(Definition $definition, $service, ?string $id, arr
}
}

private function getExpressionLanguage()
private function getExpressionLanguage(): ExpressionLanguage
{
if (null === $this->expressionLanguage) {
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
Expand All @@ -1653,7 +1653,7 @@ private function getExpressionLanguage()
return $this->expressionLanguage;
}

private function inVendors(string $path)
private function inVendors(string $path): bool
{
if (null === $this->vendors) {
$resource = new ComposerResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private function findNodes(): array
return $nodes;
}

private function cloneContainer()
private function cloneContainer(): ContainerBuilder
{
$parameterBag = new ParameterBag($this->container->getParameterBag()->all());

Expand Down
16 changes: 11 additions & 5 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ private function addServiceMethodCalls(Definition $definition, string $variableN
return $calls;
}

private function addServiceProperties(Definition $definition, string $variableName = 'instance')
private function addServiceProperties(Definition $definition, string $variableName = 'instance'): string
{
$code = '';
foreach ($definition->getProperties() as $name => $value) {
Expand Down Expand Up @@ -967,7 +967,7 @@ private function addServices(array &$services = null): string
return $publicServices.$privateServices;
}

private function generateServiceFiles(array $services)
private function generateServiceFiles(array $services): iterable
{
$definitions = $this->container->getDefinitions();
ksort($definitions);
Expand All @@ -993,7 +993,7 @@ private function generateServiceFiles(array $services)
}
}

private function addNewInstance(Definition $definition, string $return = '', string $id = null)
private function addNewInstance(Definition $definition, string $return = '', string $id = null): string
{
$tail = $return ? ";\n" : '';

Expand Down Expand Up @@ -1914,7 +1914,7 @@ private function getNextVariableName(): string
}
}

private function getExpressionLanguage()
private function getExpressionLanguage(): ExpressionLanguage
{
if (null === $this->expressionLanguage) {
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
Expand All @@ -1941,7 +1941,7 @@ private function getExpressionLanguage()
return $this->expressionLanguage;
}

private function isHotPath(Definition $definition)
private function isHotPath(Definition $definition): bool
{
return $this->hotPathTag && $definition->hasTag($this->hotPathTag) && !$definition->isDeprecated();
}
Expand All @@ -1965,6 +1965,9 @@ private function isSingleUsePrivateNode(ServiceReferenceGraphNode $node): bool
return 1 === \count($ids);
}

/**
* @return mixed
*/
private function export($value)
{
if (null !== $this->targetDirRegex && \is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
Expand All @@ -1988,6 +1991,9 @@ private function export($value)
return $this->doExport($value, true);
}

/**
* @return mixed
*/
private function doExport($value, bool $resolveEnv = false)
{
$shouldCacheValue = $resolveEnv && \is_string($value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,9 @@ private function escape(array $arguments): array
*
* @param mixed $value Value to convert
*
* @return string
*
* @throws RuntimeException When trying to dump object or resource
*/
public static function phpToXml($value)
public static function phpToXml($value): string
{
switch (true) {
case null === $value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ private function addParameters(): string
* Dumps callable to YAML format.
*
* @param mixed $callable
*
* @return mixed
*/
private function dumpCallable($callable)
{
Expand Down Expand Up @@ -299,7 +301,7 @@ private function getParameterCall(string $id): string
return sprintf('%%%s%%', $id);
}

private function getExpressionCall(string $expression)
private function getExpressionCall(string $expression): string
{
return sprintf('@=%s', $expression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ final protected function processConfiguration(ConfigurationInterface $configurat
/**
* @internal
*/
final public function getProcessedConfigs()
final public function getProcessedConfigs(): array
{
try {
return $this->processedConfigs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __destruct()
*
* @return $this
*/
final public function exclude($excludes)
final public function exclude($excludes): self
{
$this->excludes = (array) $excludes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function setDefinition($id, Definition $definition)
}
}

private function findClasses(string $namespace, string $pattern, array $excludePatterns)
private function findClasses(string $namespace, string $pattern, array $excludePatterns): array
{
$parameterBag = $this->container->getParameterBag();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function supports($resource, $type = null)
* Note that the following features are not supported:
* * strings with escaped quotes are not supported "foo\"bar";
* * string concatenation ("foo" "bar").
*
* @return mixed
*/
private function phpize(string $value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ private function processAnonymousServices(\DOMDocument $xml, string $file)
}
}

private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file, bool $isChildDefinition = false)
private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file, bool $isChildDefinition = false): array
{
$arguments = [];
foreach ($this->getChildren($node, $name) as $arg) {
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/DependencyInjection/ServiceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function __invoke($id)

/**
* @internal
*
* @return static
*/
public function withContext($externalId, Container $container)
public function withContext(string $externalId, Container $container)
{
$locator = clone $this;
$locator->externalId = $externalId;
Expand Down Expand Up @@ -127,7 +129,7 @@ private function createCircularReferenceException(string $id, array $path): Cont
return new ServiceCircularReferenceException($id, $path);
}

private function formatAlternatives(array $alternatives = null, string $separator = 'and')
private function formatAlternatives(array $alternatives = null, string $separator = 'and'): string
{
$format = '"%s"%s';
if (null === $alternatives) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testBindingsShouldBeResolved()
$this->assertSame($this->container->getParameterBag()->resolveValue('%env(BAZ)%'), $boundValue);
}

private function createContainerBuilder()
private function createContainerBuilder(): ContainerBuilder
{
$containerBuilder = new ContainerBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function testTaggedArguments()
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_tagged_argument.yml', $dumper->dump());
}

private function assertEqualYamlStructure($expected, $yaml, $message = '')
private function assertEqualYamlStructure(string $expected, string $yaml, string $message = '')
{
$parser = new Parser();

Expand Down

0 comments on commit 200eae3

Please sign in to comment.