Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Jan 22, 2022
1 parent 536d872 commit b424851
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,11 @@ public static function processFunctionCall(
$if_types[$first_var_name] = [[new IsIdentical(new TTraitString())]];
}
}
} elseif ($class_exists_check_type = self::hasEnumExistsCheck($expr)) {
} elseif (self::hasEnumExistsCheck($expr)) {
if ($first_var_name) {
if ($class_exists_check_type === 2) {
$if_types[$first_var_name] = [['enum-string']];
} else {
$if_types[$first_var_name] = [['=enum-string']];
}
$class_string = new TClassString();
$class_string->is_enum = true;
$if_types[$first_var_name] = [[new IsType($class_string)]];
}
} elseif (self::hasInterfaceExistsCheck($expr)) {
if ($first_var_name) {
Expand Down Expand Up @@ -2002,30 +2000,9 @@ protected static function hasTraitExistsCheck(PhpParser\Node\Expr\FuncCall $stmt
return 0;
}

/**
* @return 0|1|2
*/
protected static function hasEnumExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): int
protected static function hasEnumExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
{
if ($stmt->name instanceof PhpParser\Node\Name
&& strtolower($stmt->name->parts[0]) === 'enum_exists'
) {
if (!isset($stmt->getArgs()[1])) {
return 2;
}

$second_arg = $stmt->getArgs()[1]->value;

if ($second_arg instanceof PhpParser\Node\Expr\ConstFetch
&& strtolower($second_arg->name->parts[0]) === 'true'
) {
return 2;
}

return 1;
}

return 0;
return $stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'enum_exists';
}

protected static function hasInterfaceExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
Expand Down
10 changes: 9 additions & 1 deletion src/Psalm/Type/Atomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,17 @@ public static function create(
return new TObjectWithProperties([], ['__tostring' => 'string']);

case 'class-string':
return new TClassString();

case 'interface-string':
$type = new TClassString();
$type->is_interface = true;
return $type;

case 'enum-string':
return new TClassString();
$type = new TClassString();
$type->is_enum = true;
return $type;

case 'trait-string':
return new TTraitString();
Expand Down
26 changes: 21 additions & 5 deletions src/Psalm/Type/Atomic/TClassString.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class TClassString extends TString
/** @var bool */
public $is_interface = false;

/** @var bool */
public $is_enum = false;

public function __construct(string $as = 'object', ?TNamedObject $as_type = null)
{
$this->as = $as;
Expand All @@ -47,8 +50,15 @@ public function __construct(string $as = 'object', ?TNamedObject $as_type = null

public function getKey(bool $include_extra = true): string
{
return ($this->is_interface ? 'interface' : 'class')
. '-string' . ($this->as === 'object' ? '' : '<' . $this->as_type . '>');
if ($this->is_interface) {
$key = 'interface-string';
} elseif ($this->is_enum) {
$key = 'enum-string';
} else {
$key = 'class-string';
}

return $key . ($this->as === 'object' ? '' : '<' . $this->as_type . '>');
}

public function __toString(): string
Expand All @@ -58,9 +68,15 @@ public function __toString(): string

public function getId(bool $nested = false): string
{
return ($this->is_loaded ? 'loaded-' : '')
. ($this->is_interface ? 'interface' : 'class')
. '-string' . ($this->as === 'object' ? '' : '<' . $this->as_type . '>');
if ($this->is_interface) {
$key = 'interface-string';
} elseif ($this->is_enum) {
$key = 'enum-string';
} else {
$key = 'class-string';
}

return ($this->is_loaded ? 'loaded-' : '') . $key . ($this->as === 'object' ? '' : '<' . $this->as_type . '>');
}

public function getAssertionString(): string
Expand Down

0 comments on commit b424851

Please sign in to comment.