Skip to content

Commit

Permalink
Type::toPhpDocNode()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 17, 2023
1 parent e5793df commit 8633125
Show file tree
Hide file tree
Showing 53 changed files with 927 additions and 5 deletions.
7 changes: 6 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,11 @@ parameters:
count: 3
path: src/Type/Generic/TemplateConstantIntegerType.php

-
message: "#^Method PHPStan\\\\Type\\\\Generic\\\\TemplateConstantIntegerType\\:\\:toPhpDocNode\\(\\) should return PHPStan\\\\PhpDocParser\\\\Ast\\\\Type\\\\ConstTypeNode but returns PHPStan\\\\PhpDocParser\\\\Ast\\\\Type\\\\IdentifierTypeNode\\.$#"
count: 1
path: src/Type/Generic/TemplateConstantIntegerType.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
count: 3
Expand Down Expand Up @@ -1150,7 +1155,7 @@ parameters:

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Accessory\\\\AccessoryType is error\\-prone and deprecated\\. Use methods on PHPStan\\\\Type\\\\Type instead\\.$#"
count: 2
count: 3
path: src/Type/IntersectionType.php

-
Expand Down
7 changes: 7 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -472,4 +474,9 @@ public function exponentiate(Type $exponent): Type
return new ErrorType();
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('list');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -333,4 +335,9 @@ public function exponentiate(Type $exponent): Type
]);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('literal-string');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -338,4 +340,9 @@ public function exponentiate(Type $exponent): Type
]);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('non-empty-string');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -329,4 +331,9 @@ public function exponentiate(Type $exponent): Type
]);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('non-falsy-string');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -340,4 +342,9 @@ public function exponentiate(Type $exponent): Type
]);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('numeric-string');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/HasMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type\Accessory;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\Dummy\DummyMethodReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
Expand Down Expand Up @@ -193,4 +195,9 @@ public static function __set_state(array $properties): Type
return new self($properties['methodName']);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode(''); // no PHPDoc representation
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -366,4 +368,9 @@ public static function __set_state(array $properties): Type
return new self($properties['offsetType']);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode(''); // no PHPDoc representation
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
Expand Down Expand Up @@ -422,4 +424,9 @@ public static function __set_state(array $properties): Type
return new self($properties['offsetType'], $properties['valueType']);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode(''); // no PHPDoc representation
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/HasPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type\Accessory;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -155,4 +157,9 @@ public static function __set_state(array $properties): Type
return new self($properties['propertyName']);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode(''); // no PHPDoc representation
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -432,4 +434,9 @@ public static function __set_state(array $properties): Type
return new self();
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('non-empty-array');
}

}
7 changes: 7 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Accessory;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -418,4 +420,9 @@ public static function __set_state(array $properties): Type
return new self();
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode(''); // no PHPDoc representation
}

}
30 changes: 30 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace PHPStan\Type;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\TrivialParametersAcceptor;
Expand Down Expand Up @@ -670,6 +673,33 @@ public function traverse(callable $cb): Type
return $this;
}

public function toPhpDocNode(): TypeNode
{
$isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed';
$isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed';

if ($isMixedKeyType) {
if ($isMixedItemType) {
return new IdentifierTypeNode('array');
}

return new GenericTypeNode(
new IdentifierTypeNode('array'),
[
$this->itemType->toPhpDocNode(),
],
);
}

return new GenericTypeNode(
new IdentifierTypeNode('array'),
[
$this->keyType->toPhpDocNode(),
$this->itemType->toPhpDocNode(),
],
);
}

public function traverseSimultaneously(Type $right, callable $cb): Type
{
$keyType = $cb($this->keyType, $right->getIterableKeyType());
Expand Down
7 changes: 7 additions & 0 deletions src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand Down Expand Up @@ -138,6 +140,11 @@ public function exponentiate(Type $exponent): Type
return ExponentiateHelper::exponentiate($this, $exponent);
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('bool');
}

/**
* @param mixed[] $properties
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
Expand Down Expand Up @@ -515,6 +519,30 @@ public function exponentiate(Type $exponent): Type
return new ErrorType();
}

public function toPhpDocNode(): TypeNode
{
if ($this->isCommonCallable) {
return new IdentifierTypeNode('callable');
}

$parameters = [];
foreach ($this->parameters as $parameter) {
$parameters[] = new CallableTypeParameterNode(
$parameter->getType()->toPhpDocNode(),
!$parameter->passedByReference()->no(),
$parameter->isVariadic(),
$parameter->getName(),
$parameter->isOptional(),
);
}

return new CallableTypeNode(
new IdentifierTypeNode('callable'),
$parameters,
$this->returnType->toPhpDocNode(),
);
}

/**
* @param mixed[] $properties
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Type/ClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;

/** @api */
Expand Down Expand Up @@ -82,6 +84,11 @@ public function getObjectTypeOrClassStringObjectType(): Type
return new ObjectWithoutClassType();
}

public function toPhpDocNode(): TypeNode
{
return new IdentifierTypeNode('class-string');
}

/**
* @param mixed[] $properties
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Closure;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
Expand Down Expand Up @@ -577,6 +581,26 @@ public function exponentiate(Type $exponent): Type
return new ErrorType();
}

public function toPhpDocNode(): TypeNode
{
$parameters = [];
foreach ($this->parameters as $parameter) {
$parameters[] = new CallableTypeParameterNode(
$parameter->getType()->toPhpDocNode(),
!$parameter->passedByReference()->no(),
$parameter->isVariadic(),
$parameter->getName(),
$parameter->isOptional(),
);
}

return new CallableTypeNode(
new IdentifierTypeNode('Closure'),
$parameters,
$this->returnType->toPhpDocNode(),
);
}

/**
* @param mixed[] $properties
*/
Expand Down
Loading

1 comment on commit 8633125

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @MartinMystikJonas @lulco I vaguely remember you wanted something like this, to get a reconstructable string representation of a Type.

Now you can do: (string) $type->toPhpDocNode() which gives you a string that can be put into PHPDocs, and also be serialized. You can reconstruct it later with https://apiref.phpstan.org/1.11.x/PHPStan.PhpDoc.TypeStringResolver.html.

Please sign in to comment.