Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Type/Generic/TemplateBenevolentUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Generic;

use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;

/** @api */
Expand Down Expand Up @@ -50,6 +51,9 @@ public function withTypes(array $types): self
public function filterTypes(callable $filterCb): Type
{
$result = parent::filterTypes($filterCb);
if ($result instanceof NeverType) {
return $result;
}
if (!$result instanceof TemplateType) {
return TemplateTypeFactory::create(
$this->getScope(),
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Generic/TemplateUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Generic;

use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

Expand Down Expand Up @@ -37,6 +38,9 @@ public function __construct(
public function filterTypes(callable $filterCb): Type
{
$result = parent::filterTypes($filterCb);
if ($result instanceof NeverType) {
return $result;
}
if (!$result instanceof TemplateType) {
return TemplateTypeFactory::create(
$this->getScope(),
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,9 @@ public function testBug12119(): void
$this->analyse([__DIR__ . '/data/bug-12119.php'], []);
}

public function testBug14504(): void
{
$this->analyse([__DIR__ . '/data/bug-14504.php'], []);
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,10 @@ public function testBug12382(): void
]);
}

public function testBug14504(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14504-method.php'], []);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Pure/data/bug-14504-method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug14504Method;

/**
* @template T of int|string
*/
class Foo
{

/** @param T $val */
public function __construct(private $val) {}

/** @phpstan-pure */
public function toString(): string {
return (string)$this->val;
}

}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Pure/data/bug-14504.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace Bug14504;

/**
* @phpstan-pure
* @template T of int|string
* @param T $val
* @return string
*/
function stringCast($val): string {
return (string)$val;
}

/**
* @phpstan-pure
* @template T of int|string
* @param T $val
* @return string
*/
function stringConcat($val): string {
return '' . $val;
}

/**
* @phpstan-pure
* @template T of int|string
* @param T $val
* @return string
*/
function stringInterpolation($val): string {
return "$val";
}

/**
* @phpstan-pure
* @template T of int|float|bool
* @param T $val
* @return string
*/
function nonStringNonObjectCast($val): string {
return (string)$val;
}
Loading