Skip to content
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

refactor description of types in Atomic #7409

Merged
merged 1 commit into from Jan 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1553,7 +1553,7 @@ private function checkForMissingPropertyType(
$message .= ' - consider ' . str_replace(
['<array-key, mixed>', '<never, never>'],
'',
(string)$suggested_type
$suggested_type->getId(false)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -621,7 +621,7 @@ private static function getTypeFromGenericTree(
|| $generic_type_value === 'interface-string'
|| $generic_type_value === 'enum-string'
) {
$class_name = (string)$generic_params[0];
$class_name = $generic_params[0]->getId(false);

if (isset($template_type_map[$class_name])) {
$first_class = array_keys($template_type_map[$class_name])[0];
Expand Down Expand Up @@ -685,7 +685,7 @@ private static function getTypeFromGenericTree(
}

if ($generic_type_value === 'key-of') {
$param_name = (string)$generic_params[0];
$param_name = $generic_params[0]->getId(false);

if (isset($template_type_map[$param_name])) {
$defining_class = array_keys($template_type_map[$param_name])[0];
Expand Down Expand Up @@ -716,7 +716,7 @@ private static function getTypeFromGenericTree(
}

if ($generic_type_value === 'value-of') {
$param_name = (string)$generic_params[0];
$param_name = $generic_params[0]->getId(false);

$param_union_types = array_values($generic_params[0]->getAtomicTypes());

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/FunctionLikeStorage.php
Expand Up @@ -250,7 +250,7 @@ public function getSignature(bool $allow_newlines): string
array_map(
fn(FunctionLikeParameter $param): string =>
($newlines ? ' ' : '')
. ($param->type ?: 'mixed')
. ($param->type ? $param->type->getId(false) : 'mixed')
. ' $' . $param->name,
$this->params
)
Expand Down
30 changes: 22 additions & 8 deletions src/Psalm/Type/Atomic.php
Expand Up @@ -77,8 +77,6 @@

abstract class Atomic implements TypeNode
{
public const KEY = 'atomic';

/**
* Whether or not the type has been checked yet
*
Expand Down Expand Up @@ -325,6 +323,10 @@ public static function create(
return new TNamedObject($value);
}

/**
* This is the string that will be used to represent the type in Union::$types. This means that two types sharing
* the same getKey value will override themselves in an Union
*/
abstract public function getKey(bool $include_extra = true): string;

public function isNumericType(): bool
Expand Down Expand Up @@ -548,9 +550,9 @@ public function replaceClassLike(string $old, string $new): void
}
}

public function __toString(): string
final public function __toString(): string
{
return '';
return $this->getId();
}

public function __clone()
Expand All @@ -572,18 +574,27 @@ public function __clone()
}
}

public function getId(bool $nested = false): string
/**
* This is the true identifier for the type. It defaults to self::getKey() but can be overrided to be more precise
*/
public function getId(bool $exact = true, bool $nested = false): string
{
return $this->__toString();
return $this->getKey();
}

/**
* This string is used in order to transform a type into an string assertion for the assertion module
* Default to self::getId()
*/
public function getAssertionString(): string
{
return $this->getId();
}

/**
* @param array<lowercase-string, string> $aliased_classes
* Returns the detailed description of the type, either in phpdoc standard format or Psalm format depending on flag
* Default to self::getKey()
*
* @param array<lowercase-string, string> $aliased_classes
*/
public function toNamespacedString(
?string $namespace,
Expand All @@ -595,6 +606,9 @@ public function toNamespacedString(
}

/**
* Returns a string representation of the type compatible with php signature or null if the type can't be expressed
* with the given php version
*
* @param array<lowercase-string, string> $aliased_classes
*/
abstract public function toPhpString(
Expand Down
34 changes: 26 additions & 8 deletions src/Psalm/Type/Atomic/CallableTrait.php
Expand Up @@ -63,7 +63,30 @@ public function __clone()

public function getKey(bool $include_extra = true): string
{
return $this->__toString();
$param_string = '';
$return_type_string = '';

if ($this->params !== null) {
$param_string .= '(';
foreach ($this->params as $i => $param) {
if ($i) {
$param_string .= ', ';
}

$param_string .= $param->getId();
}

$param_string .= ')';
}

if ($this->return_type !== null) {
$return_type_multiple = count($this->return_type->getAtomicTypes()) > 1;
$return_type_string = ':' . ($return_type_multiple ? '(' : '')
. $this->return_type->getId() . ($return_type_multiple ? ')' : '');
}

return ($this->is_pure ? 'pure-' : ($this->is_pure === null ? '' : 'impure-'))
. $this->value . $param_string . $return_type_string;
}

/**
Expand Down Expand Up @@ -147,7 +170,7 @@ public function toPhpString(
return $this->value;
}

public function getId(bool $nested = false): string
public function getId(bool $exact = true, bool $nested = false): string
{
$param_string = '';
$return_type_string = '';
Expand All @@ -168,18 +191,13 @@ public function getId(bool $nested = false): string
if ($this->return_type !== null) {
$return_type_multiple = count($this->return_type->getAtomicTypes()) > 1;
$return_type_string = ':' . ($return_type_multiple ? '(' : '')
. $this->return_type->getId() . ($return_type_multiple ? ')' : '');
. $this->return_type->getId($exact) . ($return_type_multiple ? ')' : '');
}

return ($this->is_pure ? 'pure-' : ($this->is_pure === null ? '' : 'impure-'))
. $this->value . $param_string . $return_type_string;
}

public function __toString(): string
{
return $this->getId();
}

public function replaceTemplateTypesWithStandins(
TemplateResult $template_result,
Codebase $codebase,
Expand Down
22 changes: 3 additions & 19 deletions src/Psalm/Type/Atomic/GenericTrait.php
Expand Up @@ -21,27 +21,11 @@

trait GenericTrait
{
public function __toString(): string
public function getId(bool $exact = true, bool $nested = false): string
{
$s = '';
foreach ($this->type_params as $type_param) {
$s .= $type_param . ', ';
}

$extra_types = '';

if ($this instanceof TNamedObject && $this->extra_types) {
$extra_types = '&' . implode('&', $this->extra_types);
}

return $this->value . '<' . substr($s, 0, -2) . '>' . $extra_types;
}

public function getId(bool $nested = false): string
{
$s = '';
foreach ($this->type_params as $type_param) {
$s .= $type_param->getId() . ', ';
$s .= $type_param->getId($exact) . ', ';
}

$extra_types = '';
Expand All @@ -51,7 +35,7 @@ public function getId(bool $nested = false): string
$extra_types = '&' . implode(
'&',
array_map(
fn($type) => $type->getId(true),
fn($type) => $type->getId($exact, true),
$this->extra_types
)
);
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Type/Atomic/TArrayKey.php
Expand Up @@ -7,11 +7,6 @@
*/
class TArrayKey extends Scalar
{
public function __toString(): string
{
return 'array-key';
}

public function getKey(bool $include_extra = true): string
{
return 'array-key';
Expand Down
Empty file.
Empty file.
5 changes: 0 additions & 5 deletions src/Psalm/Type/Atomic/TBool.php
Expand Up @@ -7,11 +7,6 @@
*/
class TBool extends Scalar
{
public function __toString(): string
{
return 'bool';
}

public function getKey(bool $include_extra = true): string
{
return 'bool';
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Type/Atomic/TCallableObject.php
Expand Up @@ -7,11 +7,6 @@
*/
class TCallableObject extends TObject
{
public function __toString(): string
{
return 'callable-object';
}

public function getKey(bool $include_extra = true): string
{
return 'callable-object';
Expand Down
5 changes: 0 additions & 5 deletions src/Psalm/Type/Atomic/TCallableString.php
Expand Up @@ -13,11 +13,6 @@ public function getKey(bool $include_extra = true): string
return 'callable-string';
}

public function getId(bool $nested = false): string
{
return $this->getKey();
}

public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
Expand Down
7 changes: 1 addition & 6 deletions src/Psalm/Type/Atomic/TClassConstant.php
Expand Up @@ -27,12 +27,7 @@ public function getKey(bool $include_extra = true): string
return 'class-constant(' . $this->fq_classlike_name . '::' . $this->const_name . ')';
}

public function __toString(): string
{
return $this->fq_classlike_name . '::' . $this->const_name;
}

public function getId(bool $nested = false): string
public function getId(bool $exact = true, bool $nested = false): string
{
return $this->fq_classlike_name . '::' . $this->const_name;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Psalm/Type/Atomic/TClassString.php
Expand Up @@ -61,12 +61,7 @@ public function getKey(bool $include_extra = true): string
return $key . ($this->as === 'object' ? '' : '<' . $this->as_type . '>');
}

public function __toString(): string
{
return $this->getKey();
}

public function getId(bool $nested = false): string
public function getId(bool $exact = true, bool $nested = false): string
{
if ($this->is_interface) {
$key = 'interface-string';
Expand Down
27 changes: 5 additions & 22 deletions src/Psalm/Type/Atomic/TClassStringMap.php
Expand Up @@ -34,8 +34,6 @@ class TClassStringMap extends Atomic
*/
public $value_param;

public const KEY = 'class-string-map';

/**
* Constructs a new instance of a list
*/
Expand All @@ -46,29 +44,15 @@ public function __construct(string $param_name, ?TNamedObject $as_type, Union $v
$this->as_type = $as_type;
}

public function __toString(): string
{
/** @psalm-suppress MixedOperand */
return static::KEY
. '<'
. $this->param_name
. ' as '
. ($this->as_type ? (string) $this->as_type : 'object')
. ', '
. ((string) $this->value_param)
. '>';
}

public function getId(bool $nested = false): string
public function getId(bool $exact = true, bool $nested = false): string
{
/** @psalm-suppress MixedOperand */
return static::KEY
return 'class-string-map'
. '<'
. $this->param_name
. ' as '
. ($this->as_type ? (string) $this->as_type : 'object')
. ($this->as_type ? $this->as_type->getId($exact) : 'object')
. ', '
. $this->value_param->getId()
. $this->value_param->getId($exact)
. '>';
}

Expand Down Expand Up @@ -97,8 +81,7 @@ public function toNamespacedString(
);
}

/** @psalm-suppress MixedOperand */
return static::KEY
return 'class-string-map'
. '<'
. $this->param_name
. ($this->as_type ? ' as ' . $this->as_type : '')
Expand Down
10 changes: 0 additions & 10 deletions src/Psalm/Type/Atomic/TClosedResource.php
Expand Up @@ -9,21 +9,11 @@
*/
class TClosedResource extends Atomic
{
public function __toString(): string
{
return 'closed-resource';
}

public function getKey(bool $include_extra = true): string
{
return 'closed-resource';
}

public function getId(bool $nested = false): string
{
return 'closed-resource';
}

/**
* @param array<lowercase-string, string> $aliased_classes
*/
Expand Down