From 3652d51275f8de4a9e0468e4af033c2006f03137 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 20 Sep 2020 00:26:51 +0200 Subject: [PATCH] Remove empty() and use strict comparison when safe (#4211) * replace empty usage with stricter checks * use strict comparison when safe * replace is_null with === null for consistency --- examples/TemplateChecker.php | 2 +- examples/TemplateScanner.php | 2 +- src/Psalm/Config.php | 4 ++-- src/Psalm/Config/IssueHandler.php | 2 +- src/Psalm/DocComment.php | 2 +- src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php | 2 +- .../Statements/Expression/CallAnalyzer.php | 5 ++--- .../Statements/Expression/IncludeAnalyzer.php | 4 ++-- .../Statements/UnusedAssignmentRemover.php | 15 +++++++-------- .../Internal/LanguageServer/EmitterTrait.php | 2 +- src/Psalm/Internal/Scanner/DocblockParser.php | 2 +- src/Psalm/Internal/Scanner/ParsedDocblock.php | 4 ++-- src/Psalm/Report/CompactReport.php | 3 +-- src/Psalm/Type/Algebra.php | 2 +- src/psalm.php | 3 +-- tests/Config/ConfigTest.php | 2 +- tests/Config/Plugin/Hook/FooMethodProvider.php | 2 +- tests/EndToEnd/PsalmEndToEndTest.php | 2 +- 18 files changed, 28 insertions(+), 32 deletions(-) diff --git a/examples/TemplateChecker.php b/examples/TemplateChecker.php index 62f68bf08..42afd7f3a 100644 --- a/examples/TemplateChecker.php +++ b/examples/TemplateChecker.php @@ -23,7 +23,7 @@ class TemplateAnalyzer extends Psalm\Internal\Analyzer\FileAnalyzer $codebase = $this->project_analyzer->getCodebase(); $stmts = $codebase->getStatementsForFile($this->file_path); - if (empty($stmts)) { + if ($stmts === []) { return; } diff --git a/examples/TemplateScanner.php b/examples/TemplateScanner.php index f4ea1cb10..b684b9f27 100644 --- a/examples/TemplateScanner.php +++ b/examples/TemplateScanner.php @@ -30,7 +30,7 @@ class TemplateScanner extends Psalm\Internal\Scanner\FileScanner $progress ); - if (empty($stmts)) { + if ($stmts === []) { return; } diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 3ccf82a67..079be3af5 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -1936,8 +1936,8 @@ class Config } foreach ($objects as $object) { - if ($object != '.' && $object != '..') { - if (filetype($dir . '/' . $object) == 'dir') { + if ($object !== '.' && $object !== '..') { + if (filetype($dir . '/' . $object) === 'dir') { self::removeCacheDirectory($dir . '/' . $object); } else { unlink($dir . '/' . $object); diff --git a/src/Psalm/Config/IssueHandler.php b/src/Psalm/Config/IssueHandler.php index f9798b59e..689aece37 100644 --- a/src/Psalm/Config/IssueHandler.php +++ b/src/Psalm/Config/IssueHandler.php @@ -148,7 +148,7 @@ class IssueHandler scandir(dirname(__DIR__) . '/Issue', SCANDIR_SORT_NONE) ), function (string $issue_name): bool { - return !empty($issue_name) + return $issue_name !== '' && $issue_name !== 'MethodIssue' && $issue_name !== 'PropertyIssue' && $issue_name !== 'FunctionIssue' diff --git a/src/Psalm/DocComment.php b/src/Psalm/DocComment.php index 93fa23401..523509703 100644 --- a/src/Psalm/DocComment.php +++ b/src/Psalm/DocComment.php @@ -137,7 +137,7 @@ class DocComment $indent = 0; foreach (array_filter(explode("\n", $docblock)) as $line) { for ($ii = 0; $ii < strlen($line); ++$ii) { - if ($line[$ii] != ' ') { + if ($line[$ii] !== ' ') { break; } ++$indent; diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 94660b6b0..e47cdf3c7 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -215,7 +215,7 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSou bool $from_docblock = false ): ?bool { $codebase = $statements_source->getCodebase(); - if (empty($fq_class_name)) { + if ($fq_class_name === '') { if (IssueBuffer::accepts( new UndefinedClass( 'Class or interface does not exist', diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index d3a7a2b06..c53a5df7a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -22,7 +22,6 @@ use function strtolower; use function strpos; use function count; use function in_array; -use function is_null; use function is_string; use function preg_match; use function preg_replace; @@ -609,9 +608,9 @@ class CallAnalyzer if ($arg_var_id) { $assertion_var_id = $arg_var_id; } - } elseif ($assertion->var_id === '$this' && !is_null($thisName)) { + } elseif ($assertion->var_id === '$this' && $thisName !== null) { $assertion_var_id = $thisName; - } elseif (strpos($assertion->var_id, '$this->') === 0 && !is_null($thisName)) { + } elseif (strpos($assertion->var_id, '$this->') === 0 && $thisName !== null) { $assertion_var_id = $thisName . str_replace('$this->', '->', $assertion->var_id); } elseif (isset($context->vars_in_scope[$assertion->var_id])) { $assertion_var_id = $assertion->var_id; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php index 0b0cfe590..1e613a356 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/IncludeAnalyzer.php @@ -356,12 +356,12 @@ class IncludeAnalyzer return $file_name; } - $paths = PATH_SEPARATOR == ':' + $paths = PATH_SEPARATOR === ':' ? preg_split('#(?addVisitor($visitor); $traverser->traverse([$rhs_exp]); - $rhs_exp_trivial = (count($visitor->getNonTrivialExpr()) == 0); + $rhs_exp_trivial = (count($visitor->getNonTrivialExpr()) === 0); if ($rhs_exp_trivial) { $treat_as_expr = false; @@ -102,7 +101,7 @@ class UnusedAssignmentRemover } FileManipulationBuffer::add($original_location->file_path, [$new_file_manipulation]); - } elseif (!is_null($assign_exp)) { + } elseif ($assign_exp !== null) { $is_assign_ref = $assign_exp instanceof PhpParser\Node\Expr\AssignRef; $new_file_manipulation = self::getPartialRemovalBounds( $codebase, @@ -133,7 +132,7 @@ class UnusedAssignmentRemover $iter = 1; // Check if second token is just whitespace - if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) { + if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) === 0) { $offset_count += strlen($token_list[1][1]); $iter++; } @@ -147,7 +146,7 @@ class UnusedAssignmentRemover $iter++; // Remove any whitespace following assignment operator token (e.g "=", "+=") - if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) { + if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) === 0) { $offset_count += strlen($token_list[$iter][1]); $iter++; } @@ -157,7 +156,7 @@ class UnusedAssignmentRemover $offset_count += 1; $iter++; // Handle any whitespace after "&" - if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) == 0) { + if (is_array($token_list[$iter]) && strlen(trim($token_list[$iter][1])) === 0) { $offset_count += strlen($token_list[$iter][1]); } } @@ -249,7 +248,7 @@ class UnusedAssignmentRemover $target_exp = $search_result[0]; $levels_taken = $search_result[1]; - if (!is_null($target_exp)) { + if ($target_exp !== null) { $assign_exp_found = true; $assign_exp = $target_exp; $assign_stmt = $levels_taken === 1 ? $stmt : null; diff --git a/src/Psalm/Internal/LanguageServer/EmitterTrait.php b/src/Psalm/Internal/LanguageServer/EmitterTrait.php index dad5c60e0..e0dce85a6 100644 --- a/src/Psalm/Internal/LanguageServer/EmitterTrait.php +++ b/src/Psalm/Internal/LanguageServer/EmitterTrait.php @@ -74,7 +74,7 @@ trait EmitterTrait array $arguments = [], ?callable $continueCallBack = null ) : bool { - if (\is_null($continueCallBack)) { + if ($continueCallBack === null) { foreach ($this->listeners($eventName) as $listener) { /** @psalm-suppress MixedAssignment */ $result = \call_user_func_array($listener, $arguments); diff --git a/src/Psalm/Internal/Scanner/DocblockParser.php b/src/Psalm/Internal/Scanner/DocblockParser.php index 5fe9bdd18..3dffef3b3 100644 --- a/src/Psalm/Internal/Scanner/DocblockParser.php +++ b/src/Psalm/Internal/Scanner/DocblockParser.php @@ -95,7 +95,7 @@ class DocblockParser $min_indent = 80; foreach ($lines as $k => $line) { $indent = strspn($line, ' '); - if ($indent == strlen($line)) { + if ($indent === strlen($line)) { // This line consists of only spaces. Trim it completely. $lines[$k] = ''; continue; diff --git a/src/Psalm/Internal/Scanner/ParsedDocblock.php b/src/Psalm/Internal/Scanner/ParsedDocblock.php index d022e42e6..ea159e84a 100644 --- a/src/Psalm/Internal/Scanner/ParsedDocblock.php +++ b/src/Psalm/Internal/Scanner/ParsedDocblock.php @@ -38,7 +38,7 @@ class ParsedDocblock $trimmed_description = trim($this->description); - if (!empty($trimmed_description)) { + if ($trimmed_description !== '') { $description_lines = explode("\n", $this->description); foreach ($description_lines as $line) { @@ -47,7 +47,7 @@ class ParsedDocblock } if ($this->tags) { - if (!empty($trimmed_description)) { + if ($trimmed_description !== '') { $doc_comment_text .= $left_padding . ' *' . "\n"; } diff --git a/src/Psalm/Report/CompactReport.php b/src/Psalm/Report/CompactReport.php index 748d330be..a4952d725 100644 --- a/src/Psalm/Report/CompactReport.php +++ b/src/Psalm/Report/CompactReport.php @@ -3,7 +3,6 @@ namespace Psalm\Report; use function count; use function implode; -use function is_null; use Psalm\Config; use Psalm\Report; use function str_split; @@ -34,7 +33,7 @@ class CompactReport extends Report foreach ($this->issues_data as $i => $issue_data) { if (!$this->show_info && $issue_data->severity === Config::REPORT_INFO) { continue; - } elseif (is_null($current_file) || $current_file !== $issue_data->file_name) { + } elseif ($current_file === null || $current_file !== $issue_data->file_name) { // If we're processing a new file, then wrap up the last table and render it out. if ($buffer !== null) { $table->render(); diff --git a/src/Psalm/Type/Algebra.php b/src/Psalm/Type/Algebra.php index ffa782b13..6816be447 100644 --- a/src/Psalm/Type/Algebra.php +++ b/src/Psalm/Type/Algebra.php @@ -535,7 +535,7 @@ class Algebra $truths = []; $active_truths = []; - if (empty($clauses)) { + if ($clauses === []) { return []; } diff --git a/src/psalm.php b/src/psalm.php index 92bd8dc07..678505b40 100644 --- a/src/psalm.php +++ b/src/psalm.php @@ -54,7 +54,6 @@ use function ini_get; use const PHP_OS; use function version_compare; use const PHP_VERSION; -use function is_null; use function setlocale; use const LC_CTYPE; use function microtime; @@ -453,7 +452,7 @@ if (isset($options['generate-stubs']) && is_string($options['generate-stubs'])) // If Xdebug is enabled, restart without it $ini_handler->check(); -if (is_null($config->load_xdebug_stub) && '' !== $ini_handler->getSkippedVersion()) { +if ($config->load_xdebug_stub === null && '' !== $ini_handler->getSkippedVersion()) { $config->load_xdebug_stub = true; } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index e46ea71f3..8ddcb0358 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -1082,7 +1082,7 @@ class ConfigTest extends \Psalm\Tests\TestCase { parent::tearDown(); - if ($this->getName() == 'testTemplatedFiles') { + if ($this->getName() === 'testTemplatedFiles') { $project_root = dirname(__DIR__, 2); foreach (['1.xml', '2.xml', '3.xml', '4.xml', '5.xml', '6.xml', '7.xml', '8.xml'] as $file_name) { @unlink($project_root . DIRECTORY_SEPARATOR . $file_name); diff --git a/tests/Config/Plugin/Hook/FooMethodProvider.php b/tests/Config/Plugin/Hook/FooMethodProvider.php index 4ddfa6a43..09f677c7c 100644 --- a/tests/Config/Plugin/Hook/FooMethodProvider.php +++ b/tests/Config/Plugin/Hook/FooMethodProvider.php @@ -75,7 +75,7 @@ class FooMethodProvider implements ?string $called_fq_classlike_name = null, ?string $called_method_name_lowercase = null ): ?Type\Union { - if ($method_name_lowercase == 'magicmethod') { + if ($method_name_lowercase === 'magicmethod') { return Type::getString(); } else { return new \Psalm\Type\Union([new \Psalm\Type\Atomic\TNamedObject('NS\\Foo2')]); diff --git a/tests/EndToEnd/PsalmEndToEndTest.php b/tests/EndToEnd/PsalmEndToEndTest.php index 65bb5e956..37dd2cfa9 100644 --- a/tests/EndToEnd/PsalmEndToEndTest.php +++ b/tests/EndToEnd/PsalmEndToEndTest.php @@ -196,7 +196,7 @@ class PsalmEndToEndTest extends TestCase { $dir = opendir($src); while (false !== ($file = readdir($dir))) { - if (($file != '.') && ($file != '..')) { + if (($file !== '.') && ($file !== '..')) { $full = $src . '/' . $file; if (is_dir($full)) { self::recursiveRemoveDirectory($full);