Skip to content

Commit 9384783

Browse files
staabmondrejmirtes
authored andcommitted
More precise phpdocs in Container-Interface
1 parent b5e44f7 commit 9384783

File tree

7 files changed

+29
-34
lines changed

7 files changed

+29
-34
lines changed

build/PHPStan/Build/ServiceLocatorDynamicReturnTypeExtension.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Reflection\ParametersAcceptorSelector;
9-
use PHPStan\Type\ClassStringType;
109
use PHPStan\Type\Constant\ConstantBooleanType;
11-
use PHPStan\Type\Constant\ConstantStringType;
12-
use PHPStan\Type\Generic\GenericClassStringType;
13-
use PHPStan\Type\ObjectType;
14-
use PHPStan\Type\ObjectWithoutClassType;
1510
use PHPStan\Type\Type;
1611
use PHPStan\Type\TypeCombinator;
1712

@@ -36,25 +31,17 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3631
if (count($methodCall->getArgs()) === 0) {
3732
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
3833
}
39-
$argType = $scope->getType($methodCall->getArgs()[0]->value);
40-
if ($argType instanceof ConstantStringType) {
41-
$type = new ObjectType($argType->getValue());
42-
} elseif ($argType instanceof GenericClassStringType) {
43-
$type = $argType->getGenericType();
44-
} elseif ($argType instanceof ClassStringType) {
45-
$type = new ObjectWithoutClassType();
46-
} else {
47-
return ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
48-
}
34+
35+
$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
4936

5037
if ($methodReflection->getName() === 'getByType' && count($methodCall->getArgs()) >= 2) {
5138
$argType = $scope->getType($methodCall->getArgs()[1]->value);
5239
if ($argType instanceof ConstantBooleanType && $argType->getValue()) {
53-
$type = TypeCombinator::addNull($type);
40+
$returnType = TypeCombinator::addNull($returnType);
5441
}
5542
}
5643

57-
return $type;
44+
return $returnType;
5845
}
5946

6047
}

build/phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ parameters:
8888
stubFiles:
8989
- stubs/ReactChildProcess.stub
9090
- stubs/ReactStreams.stub
91+
- stubs/NetteDIContainer.stub
9192
services:
9293
-
9394
class: PHPStan\Build\ServiceLocatorDynamicReturnTypeExtension

build/stubs/NetteDIContainer.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Nette\DI;
4+
5+
class Container
6+
{
7+
8+
/**
9+
* @template T of object
10+
* @param class-string<T> $type
11+
* @return T
12+
*/
13+
public function getByType(string $type);
14+
15+
}

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<property name="enableObjectTypeHint" value="false"/>
5151
</properties>
5252
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
53+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint"/>
5354
</rule>
5455
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure.UnusedInheritedVariable">
5556
<exclude-pattern>src/Command/CommandHelper.php</exclude-pattern>

src/DependencyInjection/Container.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public function hasService(string $serviceName): bool;
1515
public function getService(string $serviceName);
1616

1717
/**
18-
* @param string $className
18+
* @phpstan-template T of object
19+
* @phpstan-param class-string<T> $className
20+
* @phpstan-return T
1921
* @return mixed
2022
*/
2123
public function getByType(string $className);
2224

2325
/**
24-
* @param string $className
26+
* @param class-string $className
2527
* @return string[]
2628
*/
2729
public function findServiceNamesByType(string $className): array;

src/DependencyInjection/MemoizingContainer.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,11 @@ public function hasService(string $serviceName): bool
2020
return $this->originalContainer->hasService($serviceName);
2121
}
2222

23-
/**
24-
* @param string $serviceName
25-
* @return mixed
26-
*/
2723
public function getService(string $serviceName)
2824
{
2925
return $this->originalContainer->getService($serviceName);
3026
}
3127

32-
/**
33-
* @param string $className
34-
* @return mixed
35-
*/
3628
public function getByType(string $className)
3729
{
3830
if (array_key_exists($className, $this->servicesByType)) {
@@ -65,11 +57,6 @@ public function hasParameter(string $parameterName): bool
6557
return $this->originalContainer->hasParameter($parameterName);
6658
}
6759

68-
/**
69-
* @param string $parameterName
70-
* @return mixed
71-
* @throws ParameterNotFoundException
72-
*/
7360
public function getParameter(string $parameterName)
7461
{
7562
return $this->originalContainer->getParameter($parameterName);

src/DependencyInjection/Nette/NetteContainer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function getService(string $serviceName)
3232
}
3333

3434
/**
35-
* @param string $className
35+
* @phpstan-template T of object
36+
* @phpstan-param class-string<T> $className
37+
* @phpstan-return T
3638
* @return mixed
3739
*/
3840
public function getByType(string $className)
@@ -41,7 +43,7 @@ public function getByType(string $className)
4143
}
4244

4345
/**
44-
* @param string $className
46+
* @param class-string $className
4547
* @return string[]
4648
*/
4749
public function findServiceNamesByType(string $className): array

0 commit comments

Comments
 (0)