Skip to content

Commit b2423e9

Browse files
committed
Merge remote-tracking branch 'origin/2.1.x' into 2.2.x
2 parents c40a68e + a09c3a5 commit b2423e9

11 files changed

Lines changed: 176 additions & 1 deletion

File tree

.github/workflows/e2e-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ jobs:
124124
mv src/Foo.php.orig src/Foo.php
125125
echo -n > phpstan-baseline.neon
126126
../../bin/phpstan -vvv
127+
- script: |
128+
cd e2e/bug-14514
129+
composer install
130+
../../bin/phpstan analyze bug-14515.php
127131
- script: |
128132
cd e2e/bug10449
129133
../../bin/phpstan analyze

e2e/bug-14514/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

e2e/bug-14514/autoloader.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
spl_autoload_register(function ($class) {
4+
if ($class === 'index') {
5+
throw new LogicException("Autoloader should not be called for 'index'");
6+
}
7+
});

e2e/bug-14514/bug-14515.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
throw new Exception("ERROR: " . 'index');

e2e/bug-14514/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"autoload": {
3+
"files": [ "autoloader.php" ]
4+
}
5+
}

e2e/bug-14514/composer.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analyser/ExprHandler/Helper/ImplicitToStringCallHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public function processImplicitToStringCall(Expr $expr, MutatingScope $scope): E
2828
$impurePoints = [];
2929

3030
$exprType = $scope->getType($expr);
31-
$toStringMethod = $scope->getMethodReflection($exprType, '__toString');
31+
32+
$toStringMethod = null;
33+
if (!$exprType->isObject()->no()) {
34+
$toStringMethod = $scope->getMethodReflection($exprType, '__toString');
35+
}
3236
if ($toStringMethod === null) {
3337
return new ExpressionResult(
3438
$scope,

tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,9 @@ public function testBug14504(): void
214214
$this->analyse([__DIR__ . '/data/bug-14504.php'], []);
215215
}
216216

217+
public function testBug14511(): void
218+
{
219+
$this->analyse([__DIR__ . '/data/bug-14511.php'], []);
220+
}
221+
217222
}

tests/PHPStan/Rules/Pure/PureMethodRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,10 @@ public function testBug14504(): void
383383
$this->analyse([__DIR__ . '/data/bug-14504-method.php'], []);
384384
}
385385

386+
public function testBug14511(): void
387+
{
388+
$this->treatPhpDocTypesAsCertain = true;
389+
$this->analyse([__DIR__ . '/data/bug-14511-method.php'], []);
390+
}
391+
386392
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14511Method;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @phpstan-pure
10+
* @template T of mixed
11+
* @param T $val
12+
*/
13+
public function testStringCast(mixed $val): ?string
14+
{
15+
if (is_int($val)) {
16+
return (string) $val;
17+
}
18+
return null;
19+
}
20+
21+
/**
22+
* @phpstan-pure
23+
* @template T of mixed
24+
* @param T $val
25+
*/
26+
public function testStringConcat(mixed $val): ?string
27+
{
28+
if (is_int($val)) {
29+
return 'value: ' . $val;
30+
}
31+
return null;
32+
}
33+
34+
/**
35+
* @phpstan-pure
36+
* @template T of mixed
37+
* @param T $val
38+
*/
39+
public function testEmptyNonArray(mixed $val): ?string
40+
{
41+
if (empty($val) && !\is_array($val)) {
42+
return (string) $val;
43+
}
44+
return null;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)