Skip to content

Commit

Permalink
Merge pull request vimeo#6529 from boesing/bugfix/class-constant-reco…
Browse files Browse the repository at this point in the history
…nciliation
  • Loading branch information
weirdan committed Nov 17, 2021
2 parents aabd96c + 6bf0265 commit 6097e02
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 58 deletions.
60 changes: 60 additions & 0 deletions src/Psalm/Internal/Codebase/ClassConstantByWildcardResolver.php
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Psalm\Internal\Codebase;

use Psalm\Codebase;
use Psalm\Type;
use Psalm\Type\Atomic\TMixed;

use function array_merge;
use function array_values;
use function preg_match;
use function sprintf;
use function str_replace;

/**
* @internal
*/
final class ClassConstantByWildcardResolver
{
/**
* @var \Psalm\Codebase
*/
private $codebase;

public function __construct(Codebase $codebase)
{
$this->codebase = $codebase;
}

/**
* @return list<Type\Atomic>|null
*/
public function resolve(string $class_name, string $constant_pattern): ?array
{
if (!$this->codebase->classlike_storage_provider->has($class_name)) {
return null;
}

$constant_regex_pattern = sprintf('#^%s$#', str_replace('*', '.*', $constant_pattern));

$class_like_storage = $this->codebase->classlike_storage_provider->get($class_name);
$matched_class_constant_types = [];

foreach ($class_like_storage->constants as $constant => $class_constant_storage) {
if (preg_match($constant_regex_pattern, $constant) === 0) {
continue;
}

if (! $class_constant_storage->type) {
$matched_class_constant_types[] = [new TMixed()];
continue;
}

$matched_class_constant_types[] = $class_constant_storage->type->getAtomicTypes();
}

return array_values(array_merge([], ...$matched_class_constant_types));
}
}
9 changes: 5 additions & 4 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Expand Up @@ -22,6 +22,7 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Reconciler;
use Psalm\Type\Union;

use function array_intersect_key;
Expand All @@ -33,7 +34,7 @@
use function strpos;
use function substr;

class AssertionReconciler extends \Psalm\Type\Reconciler
class AssertionReconciler extends Reconciler
{
/**
* Reconciles types
Expand All @@ -57,7 +58,7 @@ public static function reconcile(
array $template_type_map,
?CodeLocation $code_location = null,
array $suppressed_issues = [],
?int &$failed_reconciliation = 0,
?int &$failed_reconciliation = Reconciler::RECONCILIATION_OK,
bool $negated = false
) : Union {
$codebase = $statements_analyzer->getCodebase();
Expand All @@ -66,7 +67,7 @@ public static function reconcile(
$is_loose_equality = false;
$is_equality = false;
$is_negation = false;
$failed_reconciliation = 0;
$failed_reconciliation = Reconciler::RECONCILIATION_OK;

if ($assertion[0] === '!') {
$assertion = substr($assertion, 1);
Expand Down Expand Up @@ -581,7 +582,7 @@ private static function refine(
}
}

$failed_reconciliation = 2;
$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Expand Up @@ -34,7 +34,6 @@ class NegatedAssertionReconciler extends Reconciler
* @param array<string, array<string, Type\Union>> $template_type_map
* @param string[] $suppressed_issues
* @param 0|1|2 $failed_reconciliation
*
*/
public static function reconcile(
StatementsAnalyzer $statements_analyzer,
Expand Down Expand Up @@ -91,7 +90,7 @@ public static function reconcile(
if (!$existing_var_type->hasMixed()
|| $atomic instanceof Type\Atomic\TNonEmptyMixed
) {
$failed_reconciliation = 2;
$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;

if ($code_location) {
if ($existing_var_type->from_static_property) {
Expand Down Expand Up @@ -187,7 +186,7 @@ public static function reconcile(
);
}

$failed_reconciliation = 2;
$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;
}
}

Expand Down Expand Up @@ -379,7 +378,7 @@ public static function reconcile(
}
}

$failed_reconciliation = 2;
$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;

return new Type\Union([new Type\Atomic\TEmptyMixed]);
}
Expand Down

0 comments on commit 6097e02

Please sign in to comment.