1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Remove redundant code from toNamespacedString

This commit is contained in:
Matthew Brown 2019-05-31 20:21:34 -04:00
parent 8968488fe4
commit 01f4e39719
23 changed files with 185 additions and 147 deletions

View File

@ -1006,6 +1006,38 @@ abstract class Type
return ($namespace ? $namespace . '\\' : '') . $class;
}
/**
* @param array<string, string> $aliased_classes
*/
public static function getStringFromFQCLN(
string $value,
?string $namespace,
array $aliased_classes,
?string $this_class
) : string {
if ($value === $this_class) {
return 'self';
}
if ($namespace && stripos($value, $namespace . '\\') === 0) {
return preg_replace(
'/^' . preg_quote($namespace . '\\') . '/i',
'',
$value
);
}
if (!$namespace && stripos($value, '\\') === false) {
return $value;
}
if (isset($aliased_classes[strtolower($value)])) {
return $aliased_classes[strtolower($value)];
}
return '\\' . $value;
}
/**
* @param bool $from_calculation
* @param int|null $value

View File

@ -742,21 +742,22 @@ abstract class Atomic
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return $this->getKey();
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version

View File

@ -45,15 +45,16 @@ trait CallableTrait
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($use_phpdoc_format) {
if ($this instanceof TNamedObject) {
return parent::toNamespacedString($namespace, $aliased_classes, $this_class, true);
@ -112,7 +113,7 @@ trait CallableTrait
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version

View File

@ -49,15 +49,16 @@ trait GenericTrait
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
$base_value = $this instanceof TNamedObject
? parent::toNamespacedString($namespace, $aliased_classes, $this_class, $use_phpdoc_format)
: $this->value;

View File

@ -13,15 +13,16 @@ trait HasIntersectionTrait
public $extra_types;
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
private function getNamespacedIntersectionTypes($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
private function getNamespacedIntersectionTypes(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if (!$this->extra_types) {
return '';
}

View File

@ -95,15 +95,16 @@ class ObjectLike extends \Psalm\Type\Atomic
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($use_phpdoc_format) {
return $this->getGenericArrayType()->toNamespacedString(
$namespace,

View File

@ -67,8 +67,12 @@ class TClassString extends TString implements HasClassString
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->as === 'object') {
return 'class-string';
}

View File

@ -54,7 +54,7 @@ class TGenericObject extends TNamedObject
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version

View File

@ -75,8 +75,12 @@ class TKeyOfClassConstant extends Scalar
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->fq_classlike_name === 'static') {
return 'key-of<static::' . $this->const_name . '>';
}

View File

@ -75,8 +75,12 @@ class TLiteralClassString extends TLiteralString
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->value === 'static') {
return 'static::class';
}

View File

@ -57,8 +57,12 @@ class TLiteralFloat extends TFloat
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'float';
}
}

View File

@ -57,8 +57,12 @@ class TLiteralInt extends TInt
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'int';
}
}

View File

@ -69,8 +69,12 @@ class TLiteralString extends TString
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'string';
}
}

View File

@ -51,14 +51,22 @@ class TNamedObject extends Atomic
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->value === 'static') {
return 'static';
}
$intersection_types = $this->getNamespacedIntersectionTypes(
$namespace,
$aliased_classes,
@ -66,36 +74,12 @@ class TNamedObject extends Atomic
$use_phpdoc_format
);
if ($this->value === 'static') {
return 'static';
}
if ($this->value === $this_class) {
return 'self' . $intersection_types;
}
if ($namespace && stripos($this->value, $namespace . '\\') === 0) {
return preg_replace(
'/^' . preg_quote($namespace . '\\') . '/i',
'',
$this->value
) . $intersection_types;
}
if (!$namespace && stripos($this->value, '\\') === false) {
return $this->value . $intersection_types;
}
if (isset($aliased_classes[strtolower($this->value)])) {
return $aliased_classes[strtolower($this->value)] . $intersection_types;
}
return '\\' . $this->value . $intersection_types;
return Type::getStringFromFQCLN($this->value, $namespace, $aliased_classes, $this_class) . $intersection_types;
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version

View File

@ -67,14 +67,18 @@ class TObjectWithProperties extends TObject
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($use_phpdoc_format) {
return 'object';
}

View File

@ -69,39 +69,25 @@ class TScalarClassConstant extends Scalar
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->fq_classlike_name === 'static') {
return 'static::' . $this->const_name;
}
if ($this->fq_classlike_name === $this_class) {
return 'self::' . $this->const_name;
}
if ($namespace && stripos($this->fq_classlike_name, $namespace . '\\') === 0) {
return preg_replace(
'/^' . preg_quote($namespace . '\\') . '/i',
'',
$this->fq_classlike_name
) . '::' . $this->const_name;
}
if (!$namespace && stripos($this->fq_classlike_name, '\\') === false) {
return $this->fq_classlike_name . '::' . $this->const_name;
}
if (isset($aliased_classes[strtolower($this->fq_classlike_name)])) {
return $aliased_classes[strtolower($this->fq_classlike_name)] . '::' . $this->const_name;
}
return '\\' . $this->fq_classlike_name . '::' . $this->const_name;
return \Psalm\Type::getStringFromFQCLN($this->fq_classlike_name, $namespace, $aliased_classes, $this_class)
. '::'
. $this->const_name;
}
/**

View File

@ -84,8 +84,12 @@ class TTemplateIndexedAccess extends \Psalm\Type\Atomic
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return $this->getKey();
}
}

View File

@ -80,8 +80,12 @@ class TTemplateKeyOf extends TArrayKey
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'key-of<' . $this->param_name . '>';
}
}

View File

@ -90,14 +90,18 @@ class TTemplateParam extends \Psalm\Type\Atomic
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
$intersection_types = $this->getNamespacedIntersectionTypes(
$namespace,
$aliased_classes,

View File

@ -94,8 +94,12 @@ class TTemplateParamClass extends TClassString
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return $this->param_name . '::class';
}
}

View File

@ -51,8 +51,12 @@ class TTraitString extends TString
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
return 'trait-string';
}

View File

@ -69,43 +69,25 @@ class TValueOfClassConstant extends \Psalm\Type\Atomic
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
if ($this->fq_classlike_name === 'static') {
return 'value-of<static::' . $this->const_name . '>';
}
if ($this->fq_classlike_name === $this_class) {
return 'value-of<self::' . $this->const_name . '>';
}
if ($namespace && stripos($this->fq_classlike_name, $namespace . '\\') === 0) {
return 'value-of<' . preg_replace(
'/^' . preg_quote($namespace . '\\') . '/i',
'',
$this->fq_classlike_name
) . '::' . $this->const_name . '>';
}
if (!$namespace && stripos($this->fq_classlike_name, '\\') === false) {
return 'value-of<' . $this->fq_classlike_name . '::' . $this->const_name . '>';
}
if (isset($aliased_classes[strtolower($this->fq_classlike_name)])) {
return 'value-of<'
. $aliased_classes[strtolower($this->fq_classlike_name)]
. '::'
. $this->const_name
. '>';
}
return 'value-of<\\' . $this->fq_classlike_name . '::' . $this->const_name . '>';
return 'value-of<'
. \Psalm\Type::getStringFromFQCLN($this->fq_classlike_name, $namespace, $aliased_classes, $this_class)
. '>::' . $this->const_name . '>';
}
/**

View File

@ -347,15 +347,16 @@ class Union
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param bool $use_phpdoc_format
* @param array<string, string> $aliased_classes
*
* @return string
*/
public function toNamespacedString($namespace, array $aliased_classes, $this_class, $use_phpdoc_format)
{
public function toNamespacedString(
?string $namespace,
array $aliased_classes,
?string $this_class,
bool $use_phpdoc_format
) {
$printed_int = false;
$printed_float = false;
$printed_string = false;
@ -391,7 +392,7 @@ class Union
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param array<string, string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version