1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

allow static return type in PHP8 (#4641)

This commit is contained in:
orklah 2020-11-21 00:46:35 +01:00 committed by GitHub
parent 187635c488
commit 6b72599ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
54 changed files with 130 additions and 58 deletions

View File

@ -1819,7 +1819,7 @@ class ClassAnalyzer extends ClassLikeAnalyzer
$source->getFQCLN(),
true
),
$inferred_type->canBeFullyExpressedInPhp()
$inferred_type->canBeFullyExpressedInPhp($codebase->php_major_version, $codebase->php_minor_version)
);
}

View File

@ -900,7 +900,7 @@ class ReturnTypeAnalyzer
$source->getFQCLN(),
true
),
$inferred_return_type->canBeFullyExpressedInPhp(),
$inferred_return_type->canBeFullyExpressedInPhp($codebase->php_major_version, $codebase->php_minor_version),
$function_like_storage ? $function_like_storage->return_type_description : null
);
}

View File

@ -571,7 +571,7 @@ abstract class Atomic implements TypeNode
int $php_minor_version
): ?string;
abstract public function canBeFullyExpressedInPhp(): bool;
abstract public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool;
public function replaceTemplateTypesWithStandins(
TemplateResult $template_result,

View File

@ -3,7 +3,7 @@ namespace Psalm\Type\Atomic;
abstract class Scalar extends \Psalm\Type\Atomic
{
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return true;
}

View File

@ -50,7 +50,7 @@ class TArray extends \Psalm\Type\Atomic
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return $this->type_params[0]->isArrayKey() && $this->type_params[1]->isMixed();
}

View File

@ -26,7 +26,7 @@ class TArrayKey extends Scalar
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -31,7 +31,7 @@ class TAssertionFalsy extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -23,7 +23,7 @@ class TCallable extends \Psalm\Type\Atomic
return 'callable';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return $this->params === null && $this->return_type === null;
}

View File

@ -28,7 +28,7 @@ class TCallableObject extends TObject
? 'object' : null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -14,7 +14,7 @@ class TCallableString extends TString
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -96,7 +96,7 @@ class TClassString extends TString
return 'class-string<\\' . $this->as . '>';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -122,7 +122,7 @@ class TClassStringMap extends \Psalm\Type\Atomic
return 'array';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -31,7 +31,7 @@ class TClosedResource extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -11,7 +11,7 @@ class TClosure extends TNamedObject
/** @var array<string, bool> */
public $byref_uses = [];
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -124,7 +124,7 @@ class TConditional extends \Psalm\Type\Atomic
return [$this->conditional_type, $this->if_type, $this->else_type];
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -37,7 +37,7 @@ class TDependentGetClass extends TString
: 'class-string<' . $this->as_type->getId() . '>';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -21,7 +21,7 @@ class TDependentGetDebugType extends TString
$this->typeof = $typeof;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -21,7 +21,7 @@ class TDependentGetType extends TString
$this->typeof = $typeof;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -7,13 +7,13 @@ class TFalse extends TBool
{
return 'false';
}
public function getKey(bool $include_extra = true): string
{
return 'false';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -45,7 +45,7 @@ class TGenericObject extends TNamedObject
return $this->value . '<' . substr($s, 0, -2) . '>' . $extra_types;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -13,7 +13,7 @@ class THtmlEscapedString extends TString
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -66,7 +66,7 @@ class TIntMask extends TInt
return 'int-mask<' . substr($s, 0, -2) . '>';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -56,7 +56,7 @@ class TIntMaskOf extends TInt
. '>';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -91,7 +91,7 @@ class TIterable extends Atomic
: null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return $this->type_params[0]->isMixed() && $this->type_params[1]->isMixed();
}

View File

@ -49,7 +49,7 @@ class TKeyOfClassConstant extends Scalar
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -194,7 +194,7 @@ class TKeyedArray extends \Psalm\Type\Atomic
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -92,7 +92,7 @@ class TList extends \Psalm\Type\Atomic
return 'array';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -40,7 +40,7 @@ class TLiteralClassString extends TLiteralString
return 'string';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -13,7 +13,7 @@ class TLowercaseString extends TString
return 'lowercase-string';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -34,7 +34,7 @@ class TMixed extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -109,19 +109,19 @@ class TNamedObject extends Atomic
int $php_minor_version
): ?string {
if ($this->value === 'static') {
return null;
return $php_major_version >= 8 ? 'static' : null;
}
if ($this->was_static) {
return 'self';
return $php_major_version >= 8 ? 'static' : 'self';
}
return $this->toNamespacedString($namespace, $aliased_classes, $this_class, false);
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return $this->value !== 'static' && $this->was_static === false;
return ($this->value !== 'static' && $this->was_static === false) || $php_major_version >= 8;
}
public function replaceTemplateTypesWithArgTypes(

View File

@ -26,7 +26,7 @@ class TNever extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -16,7 +16,7 @@ class TNonEmptyLowercaseString extends TNonEmptyString
/**
* @return false
*/
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -26,7 +26,7 @@ class TNull extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -26,7 +26,7 @@ class TNumeric extends Scalar
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -18,7 +18,7 @@ class TNumericString extends TString
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -29,7 +29,7 @@ class TObject extends \Psalm\Type\Atomic
: null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return true;
}

View File

@ -173,7 +173,7 @@ class TObjectWithProperties extends TObject
return $this->getKey();
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -16,7 +16,7 @@ class TPositiveInt extends TInt
/**
* @return false
*/
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -26,7 +26,7 @@ class TResource extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -26,7 +26,7 @@ class TScalar extends Scalar
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -43,7 +43,7 @@ class TScalarClassConstant extends Scalar
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -56,7 +56,7 @@ class TTemplateIndexedAccess extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -61,7 +61,7 @@ class TTemplateKeyOf extends TArrayKey
/**
* @return false
*/
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -115,7 +115,7 @@ class TTemplateParam extends \Psalm\Type\Atomic
return [$this->as];
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -58,7 +58,7 @@ class TTemplateParamClass extends TClassString
return 'string';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -44,7 +44,7 @@ class TTraitString extends TString
return 'trait-string';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -13,7 +13,7 @@ class TTrue extends TBool
return 'true';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -73,7 +73,7 @@ class TTypeAlias extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -43,7 +43,7 @@ class TValueOfClassConstant extends \Psalm\Type\Atomic
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -28,7 +28,7 @@ class TVoid extends \Psalm\Type\Atomic
? $this->getKey() : null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return true;
}

View File

@ -475,7 +475,7 @@ class Union implements TypeNode
return null;
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
if (!$this->isSingleAndMaybeNullable()) {
return false;
@ -493,7 +493,7 @@ class Union implements TypeNode
$atomic_type = array_values($types)[0];
return $atomic_type->canBeFullyExpressedInPhp();
return $atomic_type->canBeFullyExpressedInPhp($php_major_version, $php_minor_version);
}
public function removeType(string $type_string): bool

View File

@ -13,7 +13,7 @@ class TSqlSelectString extends \Psalm\Type\Atomic\TLiteralString
return 'sql-select-string(' . $this->value . ')';
}
public function canBeFullyExpressedInPhp(): bool
public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
{
return false;
}

View File

@ -893,6 +893,78 @@ class MissingReturnTypeTest extends FileManipulationTest
['MissingReturnType'],
false,
],
'staticReturn5.6' => [
'<?php
class HelloWorld
{
public function sayHello()
{
return $this;
}
}',
'<?php
class HelloWorld
{
/**
* @return static
*/
public function sayHello()
{
return $this;
}
}',
'5.6',
['MissingReturnType'],
false,
true,
],
'staticReturn7.0' => [
'<?php
class HelloWorld
{
public function sayHello()
{
return $this;
}
}',
'<?php
class HelloWorld
{
/**
* @return static
*/
public function sayHello(): self
{
return $this;
}
}',
'7.0',
['MissingReturnType'],
false,
true,
],
'staticReturn8.0' => [
'<?php
class HelloWorld
{
public function sayHello()
{
return $this;
}
}',
'<?php
class HelloWorld
{
public function sayHello(): static
{
return $this;
}
}',
'8.0',
['MissingReturnType'],
false,
true,
],
];
}
}