Skip to content

Commit bb4fdfc

Browse files
committed
Replace highlight_string() stub with a return type extension
1 parent f670288 commit bb4fdfc

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

conf/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,11 @@ services:
14971497
tags:
14981498
- phpstan.broker.dynamicFunctionReturnTypeExtension
14991499

1500+
-
1501+
class: PHPStan\Type\Php\HighlightStringDynamicReturnTypeExtension
1502+
tags:
1503+
- phpstan.broker.dynamicFunctionReturnTypeExtension
1504+
15001505
-
15011506
class: PHPStan\Type\Php\IntdivThrowTypeExtension
15021507
tags:

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,9 @@ public function hasExitAsFunction(): bool
343343
return $this->versionId >= 80400;
344344
}
345345

346+
public function highlightStringDoesNotReturnFalse(): bool
347+
{
348+
return $this->versionId >= 80400;
349+
}
350+
346351
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Php\PhpVersion;
8+
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\Type\BooleanType;
10+
use PHPStan\Type\Constant\ConstantBooleanType;
11+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
12+
use PHPStan\Type\StringType;
13+
use PHPStan\Type\Type;
14+
use function count;
15+
16+
final class HighlightStringDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
17+
{
18+
19+
public function __construct(private PhpVersion $phpVersion)
20+
{
21+
}
22+
23+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
24+
{
25+
return $functionReflection->getName() === 'highlight_string';
26+
}
27+
28+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
29+
{
30+
$args = $functionCall->getArgs();
31+
if (count($args) < 2) {
32+
if ($this->phpVersion->highlightStringDoesNotReturnFalse()) {
33+
return new ConstantBooleanType(true);
34+
}
35+
36+
return new BooleanType();
37+
}
38+
39+
$returnType = $scope->getType($args[1]->value);
40+
if ($returnType->isTrue()->yes()) {
41+
return new StringType();
42+
}
43+
44+
if ($this->phpVersion->highlightStringDoesNotReturnFalse()) {
45+
return new ConstantBooleanType(true);
46+
}
47+
48+
return new BooleanType();
49+
}
50+
51+
}

stubs/core.stub

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ function highlight_file($var, bool $return = false) {}
3535
*/
3636
function show_source($var, bool $return = false) {}
3737

38-
/**
39-
* @param mixed $var
40-
* @return ($return is true ? string : bool)
41-
*/
42-
function highlight_string($var, bool $return = false) {}
43-
4438
/**
4539
* @param mixed $var
4640
* @param bool $return
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php // lint >= 8.4
2+
3+
namespace Pr1244Php84;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function foo() {
8+
/** @var string $string */
9+
$string = doFoo();
10+
11+
assertType('null', var_export());
12+
assertType('null', var_export($string));
13+
assertType('null', var_export($string, false));
14+
assertType('string', var_export($string, true));
15+
16+
assertType('true', highlight_string());
17+
assertType('true', highlight_string($string));
18+
assertType('true', highlight_string($string, false));
19+
assertType('string', highlight_string($string, true));
20+
21+
assertType('bool', highlight_file());
22+
assertType('bool', highlight_file($string));
23+
assertType('bool', highlight_file($string, false));
24+
assertType('string', highlight_file($string, true));
25+
26+
assertType('bool', show_source());
27+
assertType('bool', show_source($string));
28+
assertType('bool', show_source($string, false));
29+
assertType('string', show_source($string, true));
30+
31+
assertType('true', print_r());
32+
assertType('true', print_r($string));
33+
assertType('true', print_r($string, false));
34+
assertType('string', print_r($string, true));
35+
}

tests/PHPStan/Analyser/nsrt/pr-1244.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint < 8.4
22

33
namespace Pr1244;
44

0 commit comments

Comments
 (0)