Skip to content

Commit

Permalink
More precise phpdocs in Container-Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Nov 9, 2021
1 parent b5e44f7 commit 9384783
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 34 deletions.
21 changes: 4 additions & 17 deletions build/PHPStan/Build/ServiceLocatorDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand All @@ -36,25 +31,17 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
if (count($methodCall->getArgs()) === 0) {
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
}
$argType = $scope->getType($methodCall->getArgs()[0]->value);
if ($argType instanceof ConstantStringType) {
$type = new ObjectType($argType->getValue());
} elseif ($argType instanceof GenericClassStringType) {
$type = $argType->getGenericType();
} elseif ($argType instanceof ClassStringType) {
$type = new ObjectWithoutClassType();
} else {
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
}

$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();

if ($methodReflection->getName() === 'getByType' && count($methodCall->getArgs()) >= 2) {
$argType = $scope->getType($methodCall->getArgs()[1]->value);
if ($argType instanceof ConstantBooleanType && $argType->getValue()) {
$type = TypeCombinator::addNull($type);
$returnType = TypeCombinator::addNull($returnType);
}
}

return $type;
return $returnType;
}

}
1 change: 1 addition & 0 deletions build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ parameters:
stubFiles:
- stubs/ReactChildProcess.stub
- stubs/ReactStreams.stub
- stubs/NetteDIContainer.stub
services:
-
class: PHPStan\Build\ServiceLocatorDynamicReturnTypeExtension
Expand Down
15 changes: 15 additions & 0 deletions build/stubs/NetteDIContainer.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Nette\DI;

class Container
{

/**
* @template T of object
* @param class-string<T> $type
* @return T
*/
public function getByType(string $type);

}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<property name="enableObjectTypeHint" value="false"/>
</properties>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint"/>
</rule>
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure.UnusedInheritedVariable">
<exclude-pattern>src/Command/CommandHelper.php</exclude-pattern>
Expand Down
6 changes: 4 additions & 2 deletions src/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public function hasService(string $serviceName): bool;
public function getService(string $serviceName);

/**
* @param string $className
* @phpstan-template T of object
* @phpstan-param class-string<T> $className
* @phpstan-return T
* @return mixed
*/
public function getByType(string $className);

/**
* @param string $className
* @param class-string $className
* @return string[]
*/
public function findServiceNamesByType(string $className): array;
Expand Down
13 changes: 0 additions & 13 deletions src/DependencyInjection/MemoizingContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ public function hasService(string $serviceName): bool
return $this->originalContainer->hasService($serviceName);
}

/**
* @param string $serviceName
* @return mixed
*/
public function getService(string $serviceName)
{
return $this->originalContainer->getService($serviceName);
}

/**
* @param string $className
* @return mixed
*/
public function getByType(string $className)
{
if (array_key_exists($className, $this->servicesByType)) {
Expand Down Expand Up @@ -65,11 +57,6 @@ public function hasParameter(string $parameterName): bool
return $this->originalContainer->hasParameter($parameterName);
}

/**
* @param string $parameterName
* @return mixed
* @throws ParameterNotFoundException
*/
public function getParameter(string $parameterName)
{
return $this->originalContainer->getParameter($parameterName);
Expand Down
6 changes: 4 additions & 2 deletions src/DependencyInjection/Nette/NetteContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function getService(string $serviceName)
}

/**
* @param string $className
* @phpstan-template T of object
* @phpstan-param class-string<T> $className
* @phpstan-return T
* @return mixed
*/
public function getByType(string $className)
Expand All @@ -41,7 +43,7 @@ public function getByType(string $className)
}

/**
* @param string $className
* @param class-string $className
* @return string[]
*/
public function findServiceNamesByType(string $className): array
Expand Down

0 comments on commit 9384783

Please sign in to comment.