Skip to content

Commit

Permalink
Misc improvements (#4314)
Browse files Browse the repository at this point in the history
* extract the operation out of the loop when possible

* remove unnecessary interfaces when already inherited in parent

* simplify expressions

* avoid using alias functions

* redundant phpdoc

* unused imports
  • Loading branch information
orklah committed Oct 15, 2020
1 parent b69e28f commit ffe7874
Show file tree
Hide file tree
Showing 51 changed files with 28 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/DocComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function parse(string $docblock, ?int $line_number = null, bool $p
$min_indent = 80;
$indent = 0;
foreach (array_filter(explode("\n", $docblock)) as $line) {
for ($ii = 0; $ii < strlen($line); ++$ii) {
for ($ii = 0, $iiMax = strlen($line); $ii < $iiMax; ++$ii) {
if ($line[$ii] !== ' ') {
break;
}
Expand Down
1 change: 0 additions & 1 deletion src/Psalm/ErrorBaseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static function read(FileProvider $fileProvider, string $baselineFile): a
$baselineDoc = new \DOMDocument();
$baselineDoc->loadXML($xmlSource, LIBXML_NOBLANKS);

/** @var \DOMNodeList $filesElement */
$filesElement = $baselineDoc->getElementsByTagName('files');

if ($filesElement->length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* @internal
*/
abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSource
abstract class ClassLikeAnalyzer extends SourceAnalyzer
{
public const VISIBILITY_PUBLIC = 1;
public const VISIBILITY_PROTECTED = 2;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/FileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @internal
* @psalm-consistent-constructor
*/
class FileAnalyzer extends SourceAnalyzer implements StatementsSource
class FileAnalyzer extends SourceAnalyzer
{
use CanAlias;

Expand Down
4 changes: 0 additions & 4 deletions src/Psalm/Internal/Analyzer/FunctionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
namespace Psalm\Internal\Analyzer;

use PhpParser;
use Psalm\Internal\Codebase\InternalCallMapHandler;
use Psalm\Context;
use Psalm\Type;
use function strtolower;
use function array_values;
use function count;
use function is_string;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* @internal
*/
class NamespaceAnalyzer extends SourceAnalyzer implements StatementsSource
class NamespaceAnalyzer extends SourceAnalyzer
{
use CanAlias;

Expand Down
1 change: 0 additions & 1 deletion src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
use function array_diff;
use function strpos;
use function explode;
use function array_pop;
use function strtolower;
use function usort;
use function file_exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\AtomicTypeComparator;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Exception\DocblockParseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use function in_array;
use function array_merge;
use function array_intersect_key;
use function array_diff_key;
use function is_string;
use function strtolower;
use function array_map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use PhpParser;
use Psalm\Internal\Analyzer\ScopeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Context;
use Psalm\Internal\Scope\LoopScope;
use Psalm\Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ public static function scrapeAssertions(

if ($typed_value_position) {
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
/** @var PhpParser\Node\Expr $conditional->left */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->left,
$this_class_name,
Expand All @@ -430,7 +429,6 @@ public static function scrapeAssertions(

$expr = $conditional->right;
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
/** @var PhpParser\Node\Expr $conditional->left */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->right,
$this_class_name,
Expand Down Expand Up @@ -1117,7 +1115,6 @@ private static function scrapeEqualityAssertions(

if ($typed_value_position) {
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
/** @var PhpParser\Node\Expr $conditional->right */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->left,
$this_class_name,
Expand All @@ -1127,7 +1124,6 @@ private static function scrapeEqualityAssertions(
$other_type = $source->node_data->getType($conditional->left);
$var_type = $source->node_data->getType($conditional->right);
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
/** @var PhpParser\Node\Expr $conditional->left */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->right,
$this_class_name,
Expand Down Expand Up @@ -1805,7 +1801,6 @@ private static function scrapeInequalityAssertions(

if ($typed_value_position) {
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
/** @var PhpParser\Node\Expr $conditional->right */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->left,
$this_class_name,
Expand All @@ -1815,7 +1810,6 @@ private static function scrapeInequalityAssertions(
$other_type = $source->node_data->getType($conditional->left);
$var_type = $source->node_data->getType($conditional->right);
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
/** @var PhpParser\Node\Expr $conditional->left */
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->right,
$this_class_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ private static function taintArrayAssignment(
array $key_values
) : void {
if ($statements_analyzer->data_flow_graph
&& ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& ($statements_analyzer->data_flow_graph instanceof VariableUseGraph
|| !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues()))
) {
if (!$stmt_type->parent_nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Psalm\Type\Reconciler;
use Psalm\Internal\Type\AssertionReconciler;
use function array_merge;
use function array_intersect_key;
use function array_values;
use function array_map;
use function array_keys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\TooFewArguments;
use Psalm\Issue\TooManyArguments;
use Psalm\Issue\UndefinedVariable;
use Psalm\IssueBuffer;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeParameter;
Expand Down Expand Up @@ -619,7 +618,7 @@ public static function checkArgumentsMatch(
$function_param_count = count($function_params);

if (count($function_params) > count($args) && !$has_packed_var) {
for ($i = count($args); $i < count($function_params); $i++) {
for ($i = count($args), $iMax = count($function_params); $i < $iMax; $i++) {
if ($function_params[$i]->default_type
&& $function_params[$i]->type
&& $function_params[$i]->type->hasTemplate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function ($arg) {
);

if ($is_push && !$unpacked_args) {
for ($i = 1; $i < count($args); $i++) {
for ($i = 1, $iMax = count($args); $i < $iMax; $i++) {
$was_inside_assignment = $context->inside_assignment;

$context->inside_assignment = true;
Expand Down Expand Up @@ -185,7 +185,7 @@ function ($arg) {
return false;
}

for ($i = 1; $i < count($args); $i++) {
for ($i = 1, $iMax = count($args); $i < $iMax; $i++) {
if (ExpressionAnalyzer::analyze(
$statements_analyzer,
$args[$i]->value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;

use PhpParser;
use Psalm\Internal\Analyzer\FunctionAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\AssertionFinder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr\PreDec;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use PhpParser;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use function array_keys;
use function preg_match;
use function preg_quote;
use function array_intersect_key;

/**
* @internal
Expand Down
1 change: 0 additions & 1 deletion src/Psalm/Internal/Analyzer/Statements/GlobalAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\InvalidGlobal;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\IssueBuffer;
use function is_string;

Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
use function strlen;
use function substr;
use function array_change_key_case;
use function array_reverse;
use function trim;
use function array_column;
use function array_combine;
Expand All @@ -60,7 +59,7 @@
/**
* @internal
*/
class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
class StatementsAnalyzer extends SourceAnalyzer
{
/**
* @var SourceAnalyzer
Expand Down
3 changes: 0 additions & 3 deletions src/Psalm/Internal/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ class Clause

/**
* @param array<string, non-empty-list<string>> $possibilities
* @param bool $wedge
* @param bool $reconcilable
* @param bool $generated
* @param array<string, bool> $redefined_vars
*/
public function __construct(
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
use Psalm\Config;
use Psalm\Exception\UnpopulatedClasslikeException;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\FileManipulation\ClassDocblockManipulator;
use Psalm\Internal\Provider\ClassLikeStorageProvider;
use Psalm\Internal\Provider\FileReferenceProvider;
use Psalm\Internal\Provider\StatementsProvider;
use Psalm\Internal\Scanner\UnresolvedConstant;
use Psalm\Issue\PossiblyUnusedMethod;
use Psalm\Issue\PossiblyUnusedParam;
use Psalm\Issue\PossiblyUnusedProperty;
Expand Down
11 changes: 1 addition & 10 deletions src/Psalm/Internal/Codebase/DataFlowGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace Psalm\Internal\Codebase;

use Psalm\CodeLocation;
use Psalm\Internal\DataFlow\Path;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Internal\DataFlow\TaintSource;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\IssueBuffer;
use Psalm\Issue\TaintedInput;
use function array_merge;
use function count;
use function implode;
use function substr;
use function strlen;
use function array_intersect;
use function array_reverse;

abstract class DataFlowGraph
Expand Down Expand Up @@ -55,7 +46,7 @@ protected static function shouldIgnoreFetch(
string $expression_type,
array $previous_path_types
) : bool {
$el = \strlen($expression_type);
$el = strlen($expression_type);

if (substr($path_type, 0, $el + 7) === $expression_type . '-fetch-') {
$fetch_nesting = 0;
Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Codebase/TaintFlowGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Psalm\Internal\Codebase;

use Psalm\CodeLocation;
use Psalm\Internal\DataFlow\Path;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Internal\DataFlow\TaintSource;
use Psalm\Internal\DataFlow\DataFlowNode;
Expand All @@ -15,7 +14,6 @@
use function substr;
use function strlen;
use function array_intersect;
use function array_reverse;

class TaintFlowGraph extends DataFlowGraph
{
Expand Down
11 changes: 0 additions & 11 deletions src/Psalm/Internal/Codebase/VariableUseGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@

namespace Psalm\Internal\Codebase;

use Psalm\CodeLocation;
use Psalm\Internal\DataFlow\Path;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Internal\DataFlow\TaintSource;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\IssueBuffer;
use Psalm\Issue\TaintedInput;
use function array_merge;
use function count;
use function implode;
use function substr;
use function strlen;
use function array_intersect;
use function array_reverse;

class VariableUseGraph extends DataFlowGraph
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function __construct(string $file_path, FunctionLike $stmt, ProjectAnaly

$in_single_line_comment = $in_multi_line_comment = false;

for ($i = 0; $i < count($chars); ++$i) {
for ($i = 0, $iMax = count($chars); $i < $iMax; ++$i) {
$char = $chars[$i];

switch ($char) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpTraverser/CustomTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @internal
*/
class CustomTraverser extends \PhpParser\NodeTraverser implements NodeTraverserInterface
class CustomTraverser extends \PhpParser\NodeTraverser
{
public function __construct()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpVisitor/AssignmentMapVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @internal
*/
class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract
{
/**
* @var array<string, array<string, bool>>
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpVisitor/CheckTrivialExprVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @internal
*/
class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
class CheckTrivialExprVisitor extends PhpParser\NodeVisitorAbstract
{
/**
* @var array<int, PhpParser\Node\Expr>
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpVisitor/NodeCleanerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @internal
*/
class NodeCleanerVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
class NodeCleanerVisitor extends PhpParser\NodeVisitorAbstract
{
private $type_provider;

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpVisitor/NodeCounterVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @internal
*/
class NodeCounterVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
class NodeCounterVisitor extends PhpParser\NodeVisitorAbstract
{
/** @var int */
public $count = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PhpVisitor/OffsetShifterVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Shifts all nodes in a given AST by a set amount
*/
class OffsetShifterVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
class OffsetShifterVisitor extends PhpParser\NodeVisitorAbstract
{
/** @var int */
private $file_offset;
Expand Down
Loading

0 comments on commit ffe7874

Please sign in to comment.