Skip to content

Commit

Permalink
run & fix some phpstorm + ea ultimate inspection stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Jul 25, 2019
1 parent 3392b74 commit 6b17426
Show file tree
Hide file tree
Showing 106 changed files with 790 additions and 611 deletions.
1 change: 0 additions & 1 deletion examples/plugins/ClassUnqualifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\FileSource;
use Psalm\Plugin\Hook\AfterClassLikeExistenceCheckInterface;
use Psalm\StatementsSource;
use Psalm\Type;
Expand Down
2 changes: 0 additions & 2 deletions examples/plugins/FunctionCasingChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\IssueBuffer;
use Psalm\Issue\TypeCoercion;
use Psalm\Plugin\Hook\AfterFunctionCallAnalysisInterface;
use Psalm\Plugin\Hook\AfterMethodCallAnalysisInterface;
use Psalm\StatementsSource;
Expand Down
2 changes: 0 additions & 2 deletions examples/plugins/PreventFloatAssignmentChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\IssueBuffer;
use Psalm\Issue\TypeCoercion;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\StatementsSource;

Expand Down
12 changes: 4 additions & 8 deletions examples/plugins/StringChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\IssueBuffer;
use Psalm\Issue\TypeCoercion;
use Psalm\Plugin\Hook\AfterExpressionAnalysisInterface;
use Psalm\StatementsSource;

Expand All @@ -34,17 +32,15 @@ public static function afterExpressionAnalysis(
if ($expr instanceof PhpParser\Node\Scalar\String_) {
$class_or_class_method = '/^\\\?Psalm(\\\[A-Z][A-Za-z0-9]+)+(::[A-Za-z0-9]+)?$/';

if (strpos($statements_source->getFileName(), 'base/DefinitionManager.php') === false
&& strpos($expr->value, 'TestController') === false
if (strpos($expr->value, 'TestController') === false
&& strpos($statements_source->getFileName(), 'base/DefinitionManager.php') === false
&& preg_match($class_or_class_method, $expr->value)
) {
$absolute_class = preg_split('/[:]/', $expr->value)[0];

if (\Psalm\IssueBuffer::accepts(
new \Psalm\Issue\InvalidClass(
'Use ::class constants when representing class names',
new CodeLocation($statements_source, $expr),
$absolute_class
explode('[:]', $expr->value)[0]
),
$statements_source->getSuppressedIssues()
)) {
Expand All @@ -55,9 +51,9 @@ public static function afterExpressionAnalysis(
&& $expr->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $expr->left->class instanceof PhpParser\Node\Name
&& $expr->left->name instanceof PhpParser\Node\Identifier
&& $expr->right instanceof PhpParser\Node\Scalar\String_
&& strtolower($expr->left->name->name) === 'class'
&& !in_array(strtolower($expr->left->class->parts[0]), ['self', 'static', 'parent'])
&& $expr->right instanceof PhpParser\Node\Scalar\String_
&& preg_match('/^::[A-Za-z0-9]+$/', $expr->right->value)
) {
$method_id = ((string) $expr->left->class->getAttribute('resolvedName')) . $expr->right->value;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function addGlobalConstantType($const_id, Type\Union $type)
*/
public function getStubbedConstantType($const_id)
{
return isset(self::$stubbed_constants[$const_id]) ? self::$stubbed_constants[$const_id] : null;
return self::$stubbed_constants[$const_id] ?? null;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Psalm\Exception\ConfigException;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Plugin\Hook;
use Psalm\PluginRegistrationSocket;
use SimpleXMLElement;

class Config
Expand Down Expand Up @@ -1118,8 +1116,12 @@ public function reportIssueInFile($issue_type, $file_path)
$any_file_path_matched = false;

foreach ($dependent_files as $dependent_file_path) {
if (($codebase->analyzer->canReportIssues($dependent_file_path)
|| $project_analyzer->canReportIssues($dependent_file_path))
if (
(
$codebase->analyzer->canReportIssues($dependent_file_path)
||
$project_analyzer->canReportIssues($dependent_file_path)
)
&& !$this->mustBeIgnored($dependent_file_path)
) {
$any_file_path_matched = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Config/FileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function loadFromXMLElement(
isset($directory['useStrictTypes']) ? (string) $directory['useStrictTypes'] : ''
) === 'true';

if ($directory_path[0] === '/' && DIRECTORY_SEPARATOR === '/') {
if (DIRECTORY_SEPARATOR === '/' && $directory_path[0] === '/') {
$prospective_directory_path = $directory_path;
} else {
$prospective_directory_path = $base_dir . DIRECTORY_SEPARATOR . $directory_path;
Expand Down Expand Up @@ -210,7 +210,7 @@ public static function loadFromXMLElement(
foreach ($e->file as $file) {
$file_path = (string) $file['name'];

if ($file_path[0] === '/' && DIRECTORY_SEPARATOR === '/') {
if (DIRECTORY_SEPARATOR === '/' && $file_path[0] === '/') {
$prospective_file_path = $file_path;
} else {
$prospective_file_path = $base_dir . DIRECTORY_SEPARATOR . $file_path;
Expand Down
14 changes: 8 additions & 6 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
namespace Psalm;

use Psalm\Config;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Clause;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type\Reconciler;
use Psalm\Type;
use Psalm\Type\Union;

class Context
Expand Down Expand Up @@ -340,7 +338,7 @@ public function update(
? $end_context->vars_in_scope[$var_id]
: null;

$existing_type = isset($this->vars_in_scope[$var_id]) ? $this->vars_in_scope[$var_id] : null;
$existing_type = $this->vars_in_scope[$var_id] ?? null;

if (!$existing_type) {
if ($new_type) {
Expand Down Expand Up @@ -410,10 +408,10 @@ public function inferType(
Type\Union $inferred_type,
Codebase $codebase
) {
if ($original_type->getId() !== $inferred_type->getId()
if (array_key_exists($var_name, $function_storage->param_types)
&& !isset($this->assigned_var_ids['$' . $var_name])
&& array_key_exists($var_name, $function_storage->param_types)
&& !$function_storage->param_types[$var_name]
&& $original_type->getId() !== $inferred_type->getId()
) {
if (isset($this->possible_param_types[$var_name])) {
if (\Psalm\Internal\Analyzer\TypeAnalyzer::isContainedBy(
Expand Down Expand Up @@ -722,7 +720,11 @@ public function hasVariable($var_name, StatementsAnalyzer $statements_analyzer =

$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name);

if ($stripped_var[0] === '$' && ($stripped_var !== '$this' || $var_name !== $stripped_var)) {
if (
($stripped_var !== '$this' || $var_name !== $stripped_var)
&&
$stripped_var[0] === '$'
) {
$this->referenced_var_ids[$var_name] = true;

if (!isset($this->vars_possibly_in_scope[$var_name])
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/DocComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function parse($docblock, $line_number = null, $preserve_format =

$last = false;
foreach ($lines as $k => $line) {
if (preg_match('/^\s?@\w/i', $line)) {
if (preg_match('/^\s?@\w/', $line)) {
$last = $k;
} elseif (preg_match('/^\s*$/', $line)) {
$last = false;
Expand Down
13 changes: 8 additions & 5 deletions src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public static function checkForParadox(

foreach ($formula2_clause->possibilities as $key => $values) {
if (!$formula2_clause->generated
&& count($values) > 1
&& !isset($new_assigned_var_ids[$key])
&& count(array_unique($values)) < count($values)
&& ($values_count = count($values))
&& $values_count > 1
&& count(array_unique($values)) < $values_count
) {
if (IssueBuffer::accepts(
new ParadoxicalCondition(
Expand All @@ -94,9 +95,11 @@ public static function checkForParadox(
foreach ($negated_formula2 as $clause_a) {
if (count($negated_formula2) === 1) {
foreach ($clause_a->possibilities as $key => $values) {
if (count($values) > 1
&& !isset($new_assigned_var_ids[$key])
&& count(array_unique($values)) < count($values)
if (
!isset($new_assigned_var_ids[$key])
&& ($values_count = count($values))
&& $values_count > 1
&& count(array_unique($values)) < $values_count
) {
if (IssueBuffer::accepts(
new RedundantCondition(
Expand Down
11 changes: 7 additions & 4 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,11 @@ public function analyze(
if ($property_storage->type) {
$property_type = clone $property_storage->type;

if (!$property_type->isMixed() &&
!$property_storage->has_default &&
if (
!$property_storage->has_default
&&
!$property_type->isMixed()
&&
!$property_type->isNullable()
) {
$property_type->initialized = false;
Expand Down Expand Up @@ -883,8 +886,8 @@ private function checkPropertyInitialization(

if (!$storage->abstract
&& !$constructor_analyzer
&& isset($storage->declaring_method_ids['__construct'])
&& $class->extends
&& isset($storage->declaring_method_ids['__construct'])
) {
list($constructor_declaring_fqcln) = explode('::', $storage->declaring_method_ids['__construct']);
list($constructor_appearing_fqcln) = explode('::', $storage->appearing_method_ids['__construct']);
Expand Down Expand Up @@ -1389,8 +1392,8 @@ private function analyzeClassMethod(
);

if ($stmt->name->name !== '__construct'
&& $config->reportIssueInFile('InvalidReturnType', $source->getFilePath())
&& $class_context->self
&& $config->reportIssueInFile('InvalidReturnType', $source->getFilePath())
) {
$return_type_location = null;
$secondary_return_type_location = null;
Expand Down
62 changes: 36 additions & 26 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static function getTypeFromComment(
}

if ($comments) {
$all_vars = (isset($comments['specials']['var']) ? $comments['specials']['var'] : [])
+ (isset($comments['specials']['psalm-var']) ? $comments['specials']['psalm-var'] : []);
$all_vars = ($comments['specials']['var'] ?? [])
+ ($comments['specials']['psalm-var'] ?? []);

/** @var int $line_number */
foreach ($all_vars as $line_number => $var_line) {
Expand All @@ -69,7 +69,7 @@ public static function getTypeFromComment(
$line_parts = self::splitDocLine($var_line);

if ($line_parts && $line_parts[0]) {
if ($line_parts[0][0] === '$' && $line_parts[0] !== '$this') {
if ($line_parts[0] !== '$this' && $line_parts[0][0] === '$') {
throw new IncorrectDocblockException('Misplaced variable');
}

Expand Down Expand Up @@ -248,16 +248,14 @@ public static function extractFunctionDocblockInfo($comment, $line_number)

if (isset($comments['specials']['return']) || isset($comments['specials']['psalm-return'])) {
/** @var array<int, string> */
$return_specials = isset($comments['specials']['psalm-return'])
? $comments['specials']['psalm-return']
: $comments['specials']['return'];
$return_specials = $comments['specials']['psalm-return'] ?? $comments['specials']['return'];

self::extractReturnType((string) reset($return_specials), array_keys($return_specials)[0], $info);
}

if (isset($comments['specials']['param']) || isset($comments['specials']['psalm-param'])) {
$all_params = (isset($comments['specials']['param']) ? $comments['specials']['param'] : [])
+ (isset($comments['specials']['psalm-param']) ? $comments['specials']['psalm-param'] : []);
$all_params = ($comments['specials']['param'] ?? [])
+ ($comments['specials']['psalm-param'] ?? []);

/** @var string $param */
foreach ($all_params as $line_number => $param) {
Expand All @@ -268,9 +266,9 @@ public static function extractFunctionDocblockInfo($comment, $line_number)
}

if (count($line_parts) > 1) {
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
if ($line_parts[0][0] !== '{'
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
&& preg_match('/^&?(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& $line_parts[0][0] !== '{'
) {
$line_parts[1] = str_replace('&', '', $line_parts[1]);

Expand Down Expand Up @@ -302,9 +300,9 @@ public static function extractFunctionDocblockInfo($comment, $line_number)
}

if (count($line_parts) > 1) {
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
if ($line_parts[0][0] !== '{'
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& $line_parts[0][0] !== '{'
) {
if ($line_parts[1][0] === '&') {
$line_parts[1] = substr($line_parts[1], 1);
Expand Down Expand Up @@ -332,14 +330,20 @@ public static function extractFunctionDocblockInfo($comment, $line_number)
foreach ($comments['specials']['global'] as $line_number => $global) {
$line_parts = self::splitDocLine($global);

if (count($line_parts) === 1 && isset($line_parts[0][0]) && $line_parts[0][0] === '$') {
if (
isset($line_parts[0][0])
&&
$line_parts[0][0] === '$'
&&
count($line_parts) === 1
) {
continue;
}

if (count($line_parts) > 1) {
if (!preg_match('/\[[^\]]+\]/', $line_parts[0])
if ($line_parts[0][0] !== '{'
&& !preg_match('/\[[^\]]+\]/', $line_parts[0])
&& preg_match('/^(\.\.\.)?&?\$[A-Za-z0-9_]+,?$/', $line_parts[1])
&& $line_parts[0][0] !== '{'
) {
if ($line_parts[1][0] === '&') {
$line_parts[1] = substr($line_parts[1], 1);
Expand Down Expand Up @@ -389,14 +393,19 @@ public static function extractFunctionDocblockInfo($comment, $line_number)
}
}

if (strpos(strtolower($comments['description']), '@inheritdoc') !== false
|| isset($comments['specials']['inheritdoc']) || isset($comments['specials']['inheritDoc'])) {
if (
isset($comments['specials']['inheritdoc'])
||
isset($comments['specials']['inheritDoc'])
||
stripos($comments['description'], '@inheritdoc') !== false
) {
$info->inheritdoc = true;
}

if (isset($comments['specials']['template']) || isset($comments['specials']['psalm-template'])) {
$all_templates = (isset($comments['specials']['template']) ? $comments['specials']['template'] : [])
+ (isset($comments['specials']['psalm-template']) ? $comments['specials']['psalm-template'] : []);
$all_templates = ($comments['specials']['template'] ?? [])
+ ($comments['specials']['psalm-template'] ?? []);

foreach ($all_templates as $template_line) {
$template_type = preg_split('/[\s]+/', $template_line);
Expand Down Expand Up @@ -534,8 +543,8 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, $comm
$info = new ClassLikeDocblockComment();

if (isset($comments['specials']['template']) || isset($comments['specials']['psalm-template'])) {
$all_templates = (isset($comments['specials']['template']) ? $comments['specials']['template'] : [])
+ (isset($comments['specials']['psalm-template']) ? $comments['specials']['psalm-template'] : []);
$all_templates = ($comments['specials']['template'] ?? [])
+ ($comments['specials']['psalm-template'] ?? []);

foreach ($all_templates as $template_line) {
$template_type = preg_split('/[\s]+/', $template_line);
Expand Down Expand Up @@ -616,8 +625,8 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, $comm
}

if (isset($comments['specials']['method']) || isset($comments['specials']['psalm-method'])) {
$all_methods = (isset($comments['specials']['method']) ? $comments['specials']['method'] : [])
+ (isset($comments['specials']['psalm-method']) ? $comments['specials']['psalm-method'] : []);
$all_methods = ($comments['specials']['method'] ?? [])
+ ($comments['specials']['psalm-method'] ?? []);

foreach ($all_methods as $line_number => $method_entry) {
$method_entry = preg_replace('/[ \t]+/', ' ', trim($method_entry));
Expand Down Expand Up @@ -714,14 +723,15 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, $comm
throw new DocblockParseException('Badly-formatted @method string ' . $method_entry);
}

/** @var \PhpParser\Comment\Doc */
/** @var \PhpParser\Comment\Doc $node_doc_comment */
$node_doc_comment = $node->getDocComment();

$statements[0]->stmts[0]->setAttribute('startLine', $node_doc_comment->getLine());
$statements[0]->stmts[0]->setAttribute('startFilePos', $node_doc_comment->getFilePos());
$statements[0]->stmts[0]->setAttribute('endFilePos', $node->getAttribute('startFilePos'));

if ($doc_comment = $statements[0]->stmts[0]->getDocComment()) {
$doc_comment = $statements[0]->stmts[0]->getDocComment();
if ($doc_comment) {
$statements[0]->stmts[0]->setDocComment(
new \PhpParser\Comment\Doc(
$doc_comment->getText(),
Expand Down Expand Up @@ -754,7 +764,7 @@ public static function extractClassLikeDocblockInfo(\PhpParser\Node $node, $comm
*/
protected static function addMagicPropertyToInfo(ClassLikeDocblockComment $info, array $specials, $property_tag)
{
$magic_property_comments = isset($specials[$property_tag]) ? $specials[$property_tag] : [];
$magic_property_comments = $specials[$property_tag] ?? [];
foreach ($magic_property_comments as $line_number => $property) {
$line_parts = self::splitDocLine($property);

Expand Down
Loading

0 comments on commit 6b17426

Please sign in to comment.