1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

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
This commit is contained in:
orklah 2020-09-20 00:26:51 +02:00 committed by Daniil Gentili
parent 6a4df30868
commit 3652d51275
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
18 changed files with 28 additions and 32 deletions

View File

@ -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;
}

View File

@ -30,7 +30,7 @@ class TemplateScanner extends Psalm\Internal\Scanner\FileScanner
$progress
);
if (empty($stmts)) {
if ($stmts === []) {
return;
}

View File

@ -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);

View File

@ -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'

View File

@ -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;

View File

@ -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 <empty string> does not exist',

View File

@ -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;

View File

@ -356,12 +356,12 @@ class IncludeAnalyzer
return $file_name;
}
$paths = PATH_SEPARATOR == ':'
$paths = PATH_SEPARATOR === ':'
? preg_split('#(?<!phar):#', get_include_path())
: explode(PATH_SEPARATOR, get_include_path());
foreach ($paths as $prefix) {
$ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
$ds = substr($prefix, -1) === DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
if ($prefix === '.') {
$prefix = $current_directory;

View File

@ -16,7 +16,6 @@ use function token_get_all;
use function array_slice;
use function is_array;
use function trim;
use function is_null;
class UnusedAssignmentRemover
{
@ -42,7 +41,7 @@ class UnusedAssignmentRemover
$assign_exp = $search_result[1];
$chain_assignment = false;
if (!is_null($assign_stmt) && !is_null($assign_exp)) {
if ($assign_stmt !== null && $assign_exp !== null) {
// Check if we have to remove assignment statemnt as expression (i.e. just "$var = ")
// Consider chain of assignments
@ -63,7 +62,7 @@ class UnusedAssignmentRemover
$traverser->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;

View File

@ -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);

View File

@ -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;

View File

@ -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";
}

View File

@ -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();

View File

@ -535,7 +535,7 @@ class Algebra
$truths = [];
$active_truths = [];
if (empty($clauses)) {
if ($clauses === []) {
return [];
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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')]);

View File

@ -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);