Skip to content

Commit

Permalink
Migrate ClassLikeName options to object
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 30, 2021
1 parent 79fcf79 commit 0f5b117
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 71 deletions.
3 changes: 2 additions & 1 deletion examples/TemplateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\CodeLocation;
Expand Down Expand Up @@ -86,7 +87,7 @@ private function checkMethod(\Psalm\Internal\MethodIdentifier $method_id, PhpPar
null,
null,
[],
true
new ClassLikeNameOptions(true)
) === false
) {
return false;
Expand Down
12 changes: 7 additions & 5 deletions src/Psalm/Internal/Analyzer/AttributeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public static function analyze(
null,
null,
$suppressed_issues,
false,
false,
false,
false,
true
new ClassLikeNameOptions(
false,
false,
false,
false,
true
)
) === false) {
return;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,9 @@ private function checkImplementedInterfaces(
null,
null,
$this->getSuppressedIssues(),
false
new ClassLikeNameOptions(
false
)
) === false) {
return false;
}
Expand Down Expand Up @@ -2449,7 +2451,9 @@ private function checkParentClass(
null,
null,
$storage->suppressed_issues + $this->getSuppressedIssues(),
false
new ClassLikeNameOptions(
false
)
) === false) {
return;
}
Expand Down
29 changes: 14 additions & 15 deletions src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ public function getFunctionLikeAnalyzer(string $method_name) : ?MethodAnalyzer

/**
* @param array<string> $suppressed_issues
* @param bool $inferred - whether or not the type was inferred
*/
public static function checkFullyQualifiedClassLikeName(
StatementsSource $statements_source,
Expand All @@ -205,12 +204,12 @@ public static function checkFullyQualifiedClassLikeName(
?string $calling_fq_class_name,
?string $calling_method_id,
array $suppressed_issues,
bool $inferred = true,
bool $allow_trait = false,
bool $allow_interface = true,
bool $from_docblock = false,
bool $from_attribute = false
?ClassLikeNameOptions $options = null
): ?bool {
if ($options === null) {
$options = new ClassLikeNameOptions();
}

$codebase = $statements_source->getCodebase();
if ($fq_class_name === '') {
if (IssueBuffer::accepts(
Expand Down Expand Up @@ -257,20 +256,20 @@ public static function checkFullyQualifiedClassLikeName(

$class_exists = $codebase->classlikes->classExists(
$fq_class_name,
!$inferred ? $code_location : null,
!$options->inferred ? $code_location : null,
$calling_fq_class_name,
$calling_method_id
);
$interface_exists = $codebase->classlikes->interfaceExists(
$fq_class_name,
!$inferred ? $code_location : null,
!$options->inferred ? $code_location : null,
$calling_fq_class_name,
$calling_method_id
);

if (!$class_exists && !($interface_exists && $allow_interface)) {
if (!$allow_trait || !$codebase->classlikes->traitExists($fq_class_name, $code_location)) {
if ($from_docblock) {
if (!$class_exists && !($interface_exists && $options->allow_interface)) {
if (!$options->allow_trait || !$codebase->classlikes->traitExists($fq_class_name, $code_location)) {
if ($options->from_docblock) {
if (IssueBuffer::accepts(
new UndefinedDocblockClass(
'Docblock-defined class or interface ' . $fq_class_name . ' does not exist',
Expand All @@ -281,7 +280,7 @@ public static function checkFullyQualifiedClassLikeName(
)) {
return false;
}
} elseif ($from_attribute) {
} elseif ($options->from_attribute) {
if (IssueBuffer::accepts(
new UndefinedAttributeClass(
'Attribute class ' . $fq_class_name . ' does not exist',
Expand Down Expand Up @@ -316,7 +315,7 @@ public static function checkFullyQualifiedClassLikeName(
try {
$class_storage = $codebase->classlike_storage_provider->get($aliased_name);
} catch (\InvalidArgumentException $e) {
if (!$inferred) {
if (!$options->inferred) {
throw $e;
}

Expand All @@ -343,7 +342,7 @@ public static function checkFullyQualifiedClassLikeName(
}
}

if (!$inferred) {
if (!$options->inferred) {
if (($class_exists && !$codebase->classHasCorrectCasing($fq_class_name)) ||
($interface_exists && !$codebase->interfaceHasCorrectCasing($fq_class_name))
) {
Expand All @@ -362,7 +361,7 @@ public static function checkFullyQualifiedClassLikeName(
}
}

if (!$inferred) {
if (!$options->inferred) {
$event = new AfterClassLikeExistenceCheckEvent(
$fq_class_name,
$code_location,
Expand Down
34 changes: 34 additions & 0 deletions src/Psalm/Internal/Analyzer/ClassLikeNameOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Psalm\Internal\Analyzer;

class ClassLikeNameOptions
{
/** @var bool */
public $inferred;

/** @var bool */
public $allow_trait;

/** @var bool */
public $allow_interface;

/** @var bool */
public $from_docblock;

/** @var bool */
public $from_attribute;

public function __construct(
bool $inferred = false,
bool $allow_trait = false,
bool $allow_interface = true,
bool $from_docblock = false,
bool $from_attribute = false
) {
$this->inferred = $inferred;
$this->allow_trait = $allow_trait;
$this->allow_interface = $allow_interface;
$this->from_docblock = $from_docblock;
$this->from_attribute = $from_attribute;
}
}
10 changes: 6 additions & 4 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ public function analyze(
null,
null,
$this->suppressed_issues,
true,
false,
true,
true
new ClassLikeNameOptions(
true,
false,
true,
true
)
) === false) {
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,12 @@ public function analyze(
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
false,
false,
true,
true
new ClassLikeNameOptions(
false,
false,
true,
true
)
)) {
$input_type = new Type\Union([new TNamedObject($expected_exception)]);
$container_type = new Type\Union([new TNamedObject('Exception'), new TNamedObject('Throwable')]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpParser;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\AssignmentAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
Expand Down Expand Up @@ -609,7 +610,8 @@ public static function checkIteratorType(
new CodeLocation($statements_analyzer->getSource(), $expr),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues()
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PhpParser;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\ScopeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\DataFlow\DataFlowNode;
Expand Down Expand Up @@ -223,7 +224,7 @@ public static function analyze(
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
false
new ClassLikeNameOptions(true)
) === false) {
// fall through
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpParser;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\CodeLocation;
Expand Down Expand Up @@ -2457,8 +2458,7 @@ private static function getGetclassInequalityAssertions(
new CodeLocation($source, $whichclass_expr),
null,
null,
$source->getSuppressedIssues(),
false
$source->getSuppressedIssues()
) === false
) {
// fall through
Expand Down Expand Up @@ -3148,7 +3148,7 @@ private static function getGetclassEqualityAssertions(
null,
null,
$source->getSuppressedIssues(),
true
new ClassLikeNameOptions(true)
) === false
) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpParser;
use Psalm\Codebase;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
Expand Down Expand Up @@ -1164,7 +1165,8 @@ private static function verifyExplicitParam(
$arg_location,
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues()
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false
) {
return;
Expand All @@ -1182,7 +1184,8 @@ private static function verifyExplicitParam(
$arg_location,
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues()
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false
) {
return;
Expand Down Expand Up @@ -1258,7 +1261,8 @@ private static function verifyExplicitParam(
$arg_location,
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues()
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true)
) === false
) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PhpParser;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
Expand Down Expand Up @@ -144,10 +145,7 @@ public static function analyze(
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
true,
false,
true,
$lhs_type_part->from_docblock
new ClassLikeNameOptions(true, false, true, $lhs_type_part->from_docblock)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public static function analyze(
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
false
$statements_analyzer->getSuppressedIssues()
) === false) {
ArgumentsAnalyzer::analyze(
$statements_analyzer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PhpParser;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
Expand Down Expand Up @@ -148,9 +149,7 @@ public static function analyze(
? $context->calling_method_id
: null,
$statements_analyzer->getSuppressedIssues(),
false,
false,
false
new ClassLikeNameOptions(false, false, false)
);
}

Expand Down
Loading

0 comments on commit 0f5b117

Please sign in to comment.