Skip to content

Commit afe356f

Browse files
Merge pull request #126 from sascha-egerer/issue-125
TASK: Add dynamic return type extension for getIndpEnv
2 parents c2b9977 + 7890bdc commit afe356f

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ services:
8989
class: SaschaEgerer\PhpstanTypo3\Type\MathUtilityTypeSpecifyingExtension
9090
tags:
9191
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
92+
-
93+
class: SaschaEgerer\PhpstanTypo3\Type\GeneralUtilityGetIndpEnvDynamicReturnTypeExtension
94+
tags:
95+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
9296
-
9397
class: SaschaEgerer\PhpstanTypo3\Stubs\StubFilesExtensionLoader
9498
tags:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Type;
4+
5+
use PhpParser\Node\Arg;
6+
use PhpParser\Node\Expr\StaticCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Type\ArrayType;
10+
use PHPStan\Type\BooleanType;
11+
use PHPStan\Type\Constant\ConstantStringType;
12+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
13+
use PHPStan\Type\StringType;
14+
use PHPStan\Type\UnionType;
15+
use TYPO3\CMS\Core\Utility\GeneralUtility;
16+
17+
class GeneralUtilityGetIndpEnvDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
18+
{
19+
20+
public function getClass(): string
21+
{
22+
return GeneralUtility::class;
23+
}
24+
25+
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
26+
{
27+
return $methodReflection->getName() === 'getIndpEnv';
28+
}
29+
30+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?\PHPStan\Type\Type
31+
{
32+
$firstArgument = $methodCall->args[0];
33+
34+
if (!$firstArgument instanceof Arg) {
35+
return null;
36+
}
37+
38+
$argumentType = $scope->getType($firstArgument->value);
39+
40+
if (!$argumentType instanceof ConstantStringType) {
41+
return null;
42+
}
43+
44+
if ($argumentType->getValue() === '_ARRAY') {
45+
return new ArrayType(new StringType(), new UnionType([new StringType(), new BooleanType()]));
46+
}
47+
48+
if (in_array($argumentType->getValue(), ['TYPO3_SSL', 'TYPO3_PROXY', 'TYPO3_REV_PROXY'], true)) {
49+
return new BooleanType();
50+
}
51+
52+
return new StringType();
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\GeneralUtilityGetIndpEnvDynamicReturnTypeExtension;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
final class GeneralUtilityGetIndpEnvDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
8+
{
9+
10+
/**
11+
* @return iterable<mixed>
12+
*/
13+
public function dataFileAsserts(): iterable
14+
{
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/GeneralUtilityGetIndpEnvTest.php');
16+
}
17+
18+
/**
19+
* @dataProvider dataFileAsserts
20+
* @param string $assertType
21+
* @param string $file
22+
* @param mixed ...$args
23+
*/
24+
public function testFileAsserts(
25+
string $assertType,
26+
string $file,
27+
...$args
28+
): void
29+
{
30+
$this->assertFileAsserts($assertType, $file, ...$args);
31+
}
32+
33+
public static function getAdditionalConfigFiles(): array
34+
{
35+
return [
36+
__DIR__ . '/../../../../extension.neon',
37+
];
38+
}
39+
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Type\GeneralUtilityGetIndpEnvDynamicReturnTypeExtension\data;
4+
5+
use TYPO3\CMS\Core\Utility\GeneralUtility;
6+
use function PHPStan\Testing\assertType;
7+
8+
final class GeneralUtilityGetIndpEnvTest
9+
{
10+
11+
public function getScriptName(): void
12+
{
13+
$value = GeneralUtility::getIndpEnv('SCRIPT_NAME');
14+
assertType('string', $value);
15+
}
16+
17+
public function getSsl(): void
18+
{
19+
$value = GeneralUtility::getIndpEnv('TYPO3_SSL');
20+
assertType('bool', $value);
21+
}
22+
23+
public function getTypo3Proxy(): void
24+
{
25+
$value = GeneralUtility::getIndpEnv('TYPO3_PROXY');
26+
assertType('bool', $value);
27+
}
28+
29+
public function getArray(): void
30+
{
31+
$value = GeneralUtility::getIndpEnv('_ARRAY');
32+
assertType('array<string, bool|string>', $value);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)