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
Original file line number Diff line number Diff line change
Expand Up @@ -1407,12 +1407,13 @@ private function createStorageForFunctionLike(
} else {
$storage->final = true;
}
$storage->final_from_docblock = $classlike_storage->final_from_docblock;
} else {
$storage->final = $classlike_storage->final || $stmt->isFinal();
// a method declared final is final natively, even in a class that is final only in its docblock
$storage->final_from_docblock = $classlike_storage->final_from_docblock && !$stmt->isFinal();
}

$storage->final_from_docblock = $classlike_storage->final_from_docblock;

if ($stmt->isPrivate()) {
$storage->visibility = ClassLikeAnalyzer::VISIBILITY_PRIVATE;
} elseif ($stmt->isProtected()) {
Expand Down
159 changes: 146 additions & 13 deletions src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@
use Psalm\Codebase;
use Psalm\Internal\Codebase\ConstantTypeResolver;
use Psalm\Node\Name\VirtualFullyQualified;
use Psalm\Node\Scalar\VirtualInt;
use Psalm\Node\Scalar\VirtualString;
use Psalm\Node\Stmt\VirtualClass;
use Psalm\Node\Stmt\VirtualClassConst;
use Psalm\Node\Stmt\VirtualClassMethod;
use Psalm\Node\Stmt\VirtualInterface;
use Psalm\Node\Stmt\VirtualProperty;
use Psalm\Node\Stmt\VirtualTrait;
use Psalm\Node\VirtualConst;
use Psalm\Node\VirtualIdentifier;
use Psalm\Node\VirtualPropertyItem;
use Psalm\Storage\ClassConstantStorage;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Type;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Union;

use ReflectionProperty;
use UnexpectedValueException;
use function array_slice;
use function in_array;
use function is_int;
use function is_string;
use function rtrim;

/**
Expand All @@ -31,7 +40,7 @@
final class ClassLikeStubGenerator
{
/**
* @return PhpParser\Node\Stmt\Class_|PhpParser\Node\Stmt\Interface_|PhpParser\Node\Stmt\Trait_
* @return PhpParser\Node\Stmt\Class_|PhpParser\Node\Stmt\Interface_|PhpParser\Node\Stmt\Trait_|PhpParser\Node\Stmt\Enum_
*/
public static function getClassLikeNode(
Codebase $codebase,
Expand Down Expand Up @@ -95,17 +104,76 @@ public static function getClassLikeNode(
);
}

if ($storage->parent_class) {
$subnodes['extends'] = new VirtualFullyQualified($storage->parent_class);
} else
$subnodes['implements'] = [];

if ($storage->direct_class_interfaces) {
$subnodes['implements'] = [];
foreach ($storage->direct_class_interfaces as $direct_class_interface) {
$subnodes['implements'][] = new VirtualFullyQualified($direct_class_interface);
foreach ($storage->direct_class_interfaces as $direct_class_interface_lc => $direct_class_interface) {
// every enum implements these
if ($storage->is_enum
&& ($direct_class_interface_lc === 'unitenum' || $direct_class_interface_lc === 'backedenum')
) {
continue;
}

$subnodes['implements'][] = new VirtualFullyQualified($direct_class_interface);
}

if ($storage->is_enum) {
$case_values = [];

foreach ($storage->enum_cases as $case_name => $case_storage) {
$case_values[$case_name] = $case_storage->getValue($codebase->classlikes)?->value;
}

$case_nodes = [];

foreach ($case_values as $case_name => $case_value) {
// a backed case needs a value, and a unique one: give the cases Psalm can't resolve a placeholder
if ($case_value === null && $storage->enum_type === 'int') {
$case_value = 0;

while (in_array($case_value, $case_values, true)) {
$case_value++;
}

$case_values[$case_name] = $case_value;
} elseif ($case_value === null && $storage->enum_type === 'string') {
$case_value = StubsGenerator::UNKNOWN_VALUE . ': ' . $case_name;

while (in_array($case_value, $case_values, true)) {
$case_value .= '_';
}

$case_values[$case_name] = $case_value;
}

$case_nodes[] = new PhpParser\Node\Stmt\EnumCase(
$case_name,
is_int($case_value)
? new VirtualInt($case_value)
: (is_string($case_value) ? new VirtualString($case_value) : null),
);
}

$subnodes['stmts'] = [...$case_nodes, ...$subnodes['stmts']];
$subnodes['scalarType'] = $storage->enum_type !== null
? new VirtualIdentifier($storage->enum_type)
: null;

return new PhpParser\Node\Stmt\Enum_(
$classlike_name,
$subnodes,
$attrs,
);
}

if ($storage->parent_class) {
$subnodes['extends'] = new VirtualFullyQualified($storage->parent_class);
}

$subnodes['flags'] = ($storage->abstract ? PhpParser\Modifiers::ABSTRACT : 0)
| ($storage->final && !$storage->final_from_docblock ? PhpParser\Modifiers::FINAL : 0)
| ($storage->readonly ? PhpParser\Modifiers::READONLY : 0);

return new VirtualClass(
$classlike_name,
$subnodes,
Expand All @@ -121,6 +189,10 @@ private static function getConstantNodes(Codebase $codebase, ClassLikeStorage $s
$constant_nodes = [];

foreach ($storage->constants as $constant_name => $constant_storage) {
if (self::isInheritedConstant($codebase, $storage, $constant_name, $constant_storage)) {
continue;
}

if ($constant_storage->unresolved_node) {
$type = new Union([
ConstantTypeResolver::resolve(
Expand All @@ -134,24 +206,71 @@ private static function getConstantNodes(Codebase $codebase, ClassLikeStorage $s
throw new UnexpectedValueException('bad');
}

$value = StubsGenerator::getExpressionFromType($type);

// the placeholder of a value Psalm can't infer would not match the type
$signature_type = $value instanceof PhpParser\Node\Scalar\String_
&& $value->value === StubsGenerator::UNKNOWN_VALUE
? null
: $constant_storage->signature_type;

$constant_nodes[] = new VirtualClassConst(
[
new VirtualConst(
$constant_name,
StubsGenerator::getExpressionFromType($type)
$value
)
],
$constant_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC
($constant_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC
? PhpParser\Modifiers::PUBLIC
: ($constant_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PROTECTED
? PhpParser\Modifiers::PROTECTED
: PhpParser\Modifiers::PRIVATE)
: PhpParser\Modifiers::PRIVATE))
| ($constant_storage->final ? PhpParser\Modifiers::FINAL : 0),
[],
[],
$signature_type
? StubsGenerator::getParserTypeFromPsalmType($signature_type)
: null,
);
}

return $constant_nodes;
}

/**
* Constants of parent classes and interfaces are declared there
*
* @psalm-capabilities read-props
*/
private static function isInheritedConstant(
Codebase $codebase,
ClassLikeStorage $storage,
string $constant_name,
ClassConstantStorage $constant_storage,
): bool {
$parents = [...$storage->parent_classes, ...$storage->parent_interfaces, ...$storage->class_implements];

foreach ($parents as $parent) {
if (!$codebase->classlike_storage_provider->has($parent)) {
continue;
}

$parent_constant = $codebase->classlike_storage_provider->get($parent)->constants[$constant_name] ?? null;

if (!$parent_constant) {
continue;
}

// the constants of a built-in class read by reflection have no location
if ($parent_constant->location?->getHash() === $constant_storage->location?->getHash()) {
return true;
}
}

return false;
}

/**
* @return list<PhpParser\Node\Stmt\Property>
*/
Expand All @@ -162,6 +281,11 @@ private static function getPropertyNodes(ClassLikeStorage $storage): array
$property_nodes = [];

foreach ($storage->properties as $property_name => $property_storage) {
// declared by PHP for every enum
if ($storage->is_enum && ($property_name === 'name' || $property_name === 'value')) {
continue;
}

$flag = match ($property_storage->visibility) {
ClassLikeAnalyzer::VISIBILITY_PRIVATE => PhpParser\Modifiers::PRIVATE,
ClassLikeAnalyzer::VISIBILITY_PROTECTED => PhpParser\Modifiers::PROTECTED,
Expand All @@ -182,7 +306,10 @@ private static function getPropertyNodes(ClassLikeStorage $storage): array
}

$property_nodes[] = new VirtualProperty(
$flag | ($property_storage->is_static ? PhpParser\Modifiers::STATIC : 0),
$flag
| ($property_storage->is_static ? PhpParser\Modifiers::STATIC : 0)
// a readonly class makes its properties readonly
| ($property_storage->native_readonly && !$storage->readonly ? PhpParser\Modifiers::READONLY : 0),
[
new VirtualPropertyItem(
$property_name,
Expand Down Expand Up @@ -280,7 +407,13 @@ private static function getMethodNodes(ClassLikeStorage $storage): array {
[
'flags' => $flag
| ($method_storage->is_static ? PhpParser\Modifiers::STATIC : 0)
| ($method_storage->abstract ? PhpParser\Modifiers::ABSTRACT : 0),
| ($method_storage->abstract ? PhpParser\Modifiers::ABSTRACT : 0)
// the methods of a class written as final are final too
| ($method_storage->final
&& !($storage->final && !$storage->final_from_docblock)
&& !$method_storage->final_from_docblock
? PhpParser\Modifiers::FINAL
: 0),
'params' => StubsGenerator::getFunctionParamNodes($method_storage),
'returnType' => $method_storage->signature_return_type
? StubsGenerator::getParserTypeFromPsalmType($method_storage->signature_return_type)
Expand Down
Loading
Loading