Skip to content

Commit 0809c89

Browse files
committed
Type current_url() and previous_url() url helpers
1 parent 354af56 commit 0809c89

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎docs/type-inference.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ This extension provides the precise return type for the `fake()` function, typin
5252
record of the given model's return type (an entity, a shaped array, or a `stdClass`). The model may be passed
5353
as a class string, a model name, or a model instance.
5454
55+
### UrlHelperFunctionReturnTypeExtension
56+
57+
**Class:** `CodeIgniter\PHPStan\Type\UrlHelperFunctionReturnTypeExtension`
58+
59+
This extension resolves the `string|URI` url helpers `current_url()` and `previous_url()` from their
60+
constant `$returnObject` flag: a constant `true` returns a `CodeIgniter\HTTP\URI`, while `false` or an
61+
absent flag returns a `string`. A non-constant flag leaves the declared union in place.
62+
5563
## Dynamic Method Return Type Extensions
5664
5765
### ReflectionHelperMethodInvokerStaticReturnTypeExtension

‎extension.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ services:
128128
tags:
129129
- phpstan.broker.dynamicFunctionReturnTypeExtension
130130

131+
-
132+
class: CodeIgniter\PHPStan\Type\UrlHelperFunctionReturnTypeExtension
133+
tags:
134+
- phpstan.broker.dynamicFunctionReturnTypeExtension
135+
131136
-
132137
class: CodeIgniter\PHPStan\Type\SuperglobalsMethodDynamicReturnTypeExtension
133138
tags:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) 2023 CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\PHPStan\Type;
15+
16+
use CodeIgniter\HTTP\URI;
17+
use PhpParser\Node\Expr\FuncCall;
18+
use PHPStan\Analyser\Scope;
19+
use PHPStan\Reflection\FunctionReflection;
20+
use PHPStan\Type\Constant\ConstantBooleanType;
21+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
22+
use PHPStan\Type\ObjectType;
23+
use PHPStan\Type\StringType;
24+
use PHPStan\Type\Type;
25+
26+
/**
27+
* Resolves the `string|URI` url helpers `current_url()` and `previous_url()` from the constant
28+
* `$returnObject` flag: `true` returns a `URI`, `false` or absent a `string`.
29+
*/
30+
final class UrlHelperFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
31+
{
32+
private const SUPPORTED_FUNCTIONS = ['current_url', 'previous_url'];
33+
34+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
35+
{
36+
return in_array($functionReflection->getName(), self::SUPPORTED_FUNCTIONS, true);
37+
}
38+
39+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
40+
{
41+
$args = $functionCall->getArgs();
42+
43+
$returnObject = isset($args[0])
44+
? $scope->getType($args[0]->value)
45+
: new ConstantBooleanType(false);
46+
47+
if ($returnObject->isTrue()->yes()) {
48+
return new ObjectType(URI::class);
49+
}
50+
51+
if ($returnObject->isFalse()->yes()) {
52+
return new StringType();
53+
}
54+
55+
return null;
56+
}
57+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) 2023 CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\PHPStan\Tests\Type;
15+
16+
use function PHPStan\Testing\assertType;
17+
18+
function test_url_helper_functions(bool $flag): void
19+
{
20+
// A false or absent flag returns a string, a constant true returns a URI.
21+
assertType('string', current_url());
22+
assertType('string', current_url(false));
23+
assertType('CodeIgniter\HTTP\URI', current_url(true));
24+
25+
assertType('string', previous_url());
26+
assertType('string', previous_url(false));
27+
assertType('CodeIgniter\HTTP\URI', previous_url(true));
28+
29+
// A non-constant flag leaves the declared union.
30+
assertType('CodeIgniter\HTTP\URI|string', current_url($flag));
31+
assertType('CodeIgniter\HTTP\URI|string', previous_url($flag));
32+
}

0 commit comments

Comments
 (0)