Skip to content

property-of<...> Utility type #3849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\OffsetAccessType;
use PHPStan\Type\PropertyOfType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
Expand Down Expand Up @@ -760,7 +761,15 @@ static function (string $variance): TemplateTypeVariance {
}

return new ErrorType();
} elseif ($mainTypeName === 'int-mask-of') {
} elseif ($mainTypeName === 'property-of') {
if (count($genericTypes) === 1) { // property-of<ValueType>
$type = new PropertyOfType($genericTypes[0]);

return $type->isResolvable() ? $type->resolve() : $type;
}

return new ErrorType();
} elseif ($mainTypeName === 'int-mask-of') {
if (count($genericTypes) === 1) { // int-mask-of<Class::CONST*>
$maskType = $this->expandIntMaskToType($genericTypes[0]);
if ($maskType !== null) {
Expand Down
112 changes: 112 additions & 0 deletions src/Type/PropertyOfType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace PHPStan\Type;

use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;

class PropertyOfType implements CompoundType, LateResolvableType
{

use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;

public function __construct(private Type $type)
{
}

public function getType(): Type
{
return $this->type;
}

public function getReferencedClasses(): array
{
return $this->type->getReferencedClasses();
}

public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return $this->type->getReferencedTemplateTypes($positionVariance);
}

public function equals(Type $type): bool
{
return $type instanceof self
&& $this->type->equals($type->type);
}

public function describe(VerbosityLevel $level): string
{
return sprintf('property-of<%s>', $this->type->describe($level));
}

public function isResolvable(): bool
{
return !TypeUtils::containsTemplateType($this->type);
}

protected function getResult(): Type
{

$classReflections = $this->type->getObjectClassReflections();
$classReflection = $classReflections[0] ?? null;

if ($classReflection !== null) {

$propertiesReflection = $classReflection->getNativeReflection()->getProperties();

// get the names of the properties
// and build a union type from them
$propertyNames = array_map(
fn($property) => new ConstantStringType($property->getName()),
$propertiesReflection
);

return new UnionType($propertyNames);

}

return new MixedType();
}

/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$type = $cb($this->type);

if ($this->type === $type) {
return $this;
}

return new self($type);
}

public function traverseSimultaneously(Type $right, callable $cb): Type
{
if (!$right instanceof self) {
return $this;
}

$type = $cb($this->type, $right->type);

if ($this->type === $type) {
return $this;
}

return new self($type);
}

public function toPhpDocNode(): TypeNode
{
return new GenericTypeNode(new IdentifierTypeNode('property-of'), [$this->type->toPhpDocNode()]);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/property-of.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace PropertyOfType;

use function PHPStan\Testing\assertType;

class Foo
{

public int $age;
public string $name;

/**
* @param property-of<self> $property
*/
public static function fromObject(string $property): void
{
assertType("'age'|'name'", $property);
}

/**
* @param property-of<static> $property
*/
public static function fromStatic(string $property): void
{
assertType("'age'|'name'", $property);
}


/**
* @param property-of<Foo> $property
*/
public static function fromClass(string $property): void
{
assertType("'age'|'name'", $property);
}

}
Loading