Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,12 @@ parameters:
count: 2
path: src/Type/Generic/TemplateMixedType.php

-
message: '#^Doing instanceof PHPStan\\Type\\IntersectionType is error\-prone and deprecated\.$#'
identifier: phpstanApi.instanceofType
count: 3
path: src/Type/Generic/TemplateNullType.php

-
message: '#^Doing instanceof PHPStan\\Type\\IntersectionType is error\-prone and deprecated\.$#'
identifier: phpstanApi.instanceofType
Expand Down Expand Up @@ -1335,6 +1341,12 @@ parameters:
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
message: '#^Doing instanceof PHPStan\\Type\\NullType is error\-prone and deprecated\. Use Type\:\:isNull\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Type/Generic/TemplateTypeFactory.php

-
message: '#^Doing instanceof PHPStan\\Type\\ObjectShapeType is error\-prone and deprecated\. Use Type\:\:isObject\(\) and Type\:\:hasProperty\(\) instead\.$#'
identifier: phpstanApi.instanceofType
Expand Down
37 changes: 37 additions & 0 deletions src/Type/Generic/TemplateNullType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\Type\NullType;
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
use PHPStan\Type\Type;

final class TemplateNullType extends NullType implements TemplateType
{

/** @use TemplateTypeTrait<NullType> */
use TemplateTypeTrait;
use UndecidedComparisonCompoundTypeTrait;

/**
* @param non-empty-string $name
*/
public function __construct(
TemplateTypeScope $scope,
TemplateTypeStrategy $templateTypeStrategy,
TemplateTypeVariance $templateTypeVariance,
string $name,
NullType $bound,
?Type $default,
)
{
parent::__construct();
$this->scope = $scope;
$this->strategy = $templateTypeStrategy;
$this->variance = $templateTypeVariance;
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
}

}
5 changes: 5 additions & 0 deletions src/Type/Generic/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\IterableType;
use PHPStan\Type\KeyOfType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectShapeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
Expand Down Expand Up @@ -112,6 +113,10 @@ public static function create(TemplateTypeScope $scope, string $name, ?Type $bou
return new TemplateIterableType($scope, $strategy, $variance, $name, $bound, $default);
}

if ($bound instanceof NullType && ($boundClass === NullType::class || $bound instanceof TemplateType)) {
return new TemplateNullType($scope, $strategy, $variance, $name, $bound, $default);
}

return new TemplateMixedType($scope, $strategy, $variance, $name, new MixedType(true), $default);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,11 @@ public function testPropertyHooks(): void
]);
}


#[RequiresPhp('>= 8.1')]
public function testBug13048(): void
{
$this->analyse([__DIR__ . '/data/bug-13048.php'], []);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13048.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php // lint >= 8.1

namespace Bug13048;

enum IndexBy {
case A;
case B;
}

/**
* @template T of IndexBy|null
* @param T $indexBy
*/
function run(?IndexBy $indexBy = null): ?string
{
return match ($indexBy) {
IndexBy::A => 'by A',
IndexBy::B => 'by B',
null => null,
};
}
Loading