Skip to content
Open
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
24 changes: 12 additions & 12 deletions src/Rules/Classes/InstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
public function processNode(Node $node, Scope $scope): array
{
$errors = [];
foreach ($this->getClassNames($node, $scope) as [$class, $isName]) {
$errors = array_merge($errors, $this->checkClassName($class, $isName, $node, $scope));
foreach ($this->getClassNames($node, $scope) as [$class, $isName, $isFromClassString]) {
$errors = array_merge($errors, $this->checkClassName($class, $isName, $isFromClassString, $node, $scope));
}
return $errors;
}
Expand All @@ -71,7 +71,7 @@
* @param Node\Expr\New_ $node
* @return list<IdentifierRuleError>
*/
private function checkClassName(string $class, bool $isName, Node $node, Scope $scope): array
private function checkClassName(string $class, bool $isName, bool $isFromClassString, Node $node, Scope $scope): array
{
$lowercasedClass = strtolower($class);
$messages = [];
Expand Down Expand Up @@ -180,7 +180,7 @@
];
}

if (!$isStatic && $classReflection->isAbstract() && $isName) {
if (!$isStatic && $classReflection->isAbstract() && $isName && !$isFromClassString) {
return [
RuleErrorBuilder::message(
sprintf('Instantiated class %s is abstract.', $classReflection->getDisplayName()),
Expand Down Expand Up @@ -274,12 +274,12 @@

/**
* @param Node\Expr\New_ $node
* @return array<int, array{string, bool}>
* @return array<int, array{string, bool, bool}>
*/
private function getClassNames(Node $node, Scope $scope): array
{
if ($node->class instanceof Node\Name) {
return [[(string) $node->class, true]];
return [[(string) $node->class, true, false]];
}

if ($node->class instanceof Node\Stmt\Class_) {
Expand All @@ -289,34 +289,34 @@
}

return array_map(
static fn (string $className) => [$className, true],
static fn (string $className) => [$className, true, false],
$classNames,
);
}

$type = $scope->getType($node->class);

if ($type->isClassString()->yes()) {
if ($type->isClassString()->yes() && count($type->getConstantStrings()) === 0) {

Check warning on line 299 in src/Rules/Classes/InstantiationRule.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $type = $scope->getType($node->class); - if ($type->isClassString()->yes() && count($type->getConstantStrings()) === 0) { + if (!$type->isClassString()->no() && count($type->getConstantStrings()) === 0) { $concretes = array_filter( $type->getClassStringObjectType()->getObjectClassReflections(), static fn (ClassReflection $classReflection): bool => !$classReflection->isInterface(),

Check warning on line 299 in src/Rules/Classes/InstantiationRule.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $type = $scope->getType($node->class); - if ($type->isClassString()->yes() && count($type->getConstantStrings()) === 0) { + if (!$type->isClassString()->no() && count($type->getConstantStrings()) === 0) { $concretes = array_filter( $type->getClassStringObjectType()->getObjectClassReflections(), static fn (ClassReflection $classReflection): bool => !$classReflection->isInterface(),
$concretes = array_filter(
$type->getClassStringObjectType()->getObjectClassReflections(),
static fn (ClassReflection $classReflection): bool => !$classReflection->isAbstract() && !$classReflection->isInterface(),
static fn (ClassReflection $classReflection): bool => !$classReflection->isInterface(),
);

if (count($concretes) > 0) {
return array_map(
static fn (ClassReflection $classReflection): array => [$classReflection->getName(), true],
static fn (ClassReflection $classReflection): array => [$classReflection->getName(), true, true],
$concretes,
);
}
}

return array_merge(
array_map(
static fn (ConstantStringType $type): array => [$type->getValue(), true],
static fn (ConstantStringType $type): array => [$type->getValue(), true, false],
$type->getConstantStrings(),
),
array_map(
static fn (string $name): array => [$name, false],
static fn (string $name): array => [$name, false, false],
$type->getObjectClassNames(),
),
);
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,18 @@ public function testClassString(): void
'Parameter #1 $i of class ClassString\A constructor expects int, string given.',
67,
],
[
'Parameter #1 $i of class ClassString\B constructor expects int, string given.',
70,
],
[
'Parameter #1 $i of class ClassString\B constructor expects int, string given.',
71,
],
[
'Parameter #1 $i of class ClassString\B constructor expects int, string given.',
72,
],
[
'Parameter #1 $i of class ClassString\C constructor expects int, string given.',
75,
Expand Down Expand Up @@ -534,6 +546,16 @@ public function testClassString(): void
]);
}

public function testBug14102(): void
{
$this->analyse([__DIR__ . '/data/bug-14102.php'], [
[
'Class Bug14102\HelloWorld constructor invoked with 0 parameters, 2 required.',
24,
],
]);
}

public function testInternalConstructor(): void
{
$this->analyse([__DIR__ . '/data/internal-constructor.php'], [
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-14102.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug14102;

abstract class HelloWorld
{
public int $c;

public function __construct(int $a, int $b){
$this->c = $a + $b;
}
}

class ChildWorld extends HelloWorld
{}

class HelloWorldFactory
{
/**
* @param class-string<HelloWorld> $className
*/
public function create(string $className): HelloWorld
{
return new $className();
}
}

$a = (new HelloWorldFactory())->create(ChildWorld::class);
Loading