| Conditions | 49 |
| Paths | 27 |
| Total Lines | 132 |
| Code Lines | 85 |
| Lines | 44 |
| Ratio | 33.33 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 39 | public function enterNode(Node $node) { |
||
| 40 | /** |
||
| 41 | * Check tables |
||
| 42 | */ |
||
| 43 | if ($this->schemaVariableName !== null && |
||
| 44 | $node instanceof Node\Expr\Assign && |
||
|
|
|||
| 45 | $node->var instanceof Node\Expr\Variable && |
||
| 46 | $node->expr instanceof Node\Expr\MethodCall && |
||
| 47 | $node->expr->var instanceof Node\Expr\Variable && |
||
| 48 | $node->expr->var->name === $this->schemaVariableName) { |
||
| 49 | |||
| 50 | if ($node->expr->name === 'createTable') { |
||
| 51 | if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { |
||
| 52 | if (!$this->checkNameLength($node->expr->args[0]->value->value)) { |
||
| 53 | $this->errors[] = [ |
||
| 54 | 'line' => $node->getLine(), |
||
| 55 | 'disallowedToken' => $node->expr->args[0]->value->value, |
||
| 56 | 'reason' => 'Table name is too long (max. 27)', |
||
| 57 | ]; |
||
| 58 | } else { |
||
| 59 | $this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } else if ($node->expr->name === 'getTable') { |
||
| 63 | if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { |
||
| 64 | $this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } else if ($this->schemaVariableName !== null && |
||
| 68 | $node instanceof Node\Expr\MethodCall && |
||
| 69 | $node->var instanceof Node\Expr\Variable && |
||
| 70 | $node->var->name === $this->schemaVariableName) { |
||
| 71 | |||
| 72 | View Code Duplication | if ($node->name === 'renameTable') { |
|
| 73 | $this->errors[] = [ |
||
| 74 | 'line' => $node->getLine(), |
||
| 75 | 'disallowedToken' => 'Deprecated method', |
||
| 76 | 'reason' => sprintf( |
||
| 77 | '`$%s->renameTable()` must not be used', |
||
| 78 | $node->var->name |
||
| 79 | ), |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Check columns and Indexes |
||
| 85 | */ |
||
| 86 | } else if (!empty($this->tableVariableNames) && |
||
| 87 | $node instanceof Node\Expr\MethodCall && |
||
| 88 | $node->var instanceof Node\Expr\Variable && |
||
| 89 | isset($this->tableVariableNames[$node->var->name])) { |
||
| 90 | |||
| 91 | if ($node->name === 'addColumn' || $node->name === 'changeColumn') { |
||
| 92 | if (isset($node->args[0]) && $node->args[0]->value instanceof Node\Scalar\String_) { |
||
| 93 | if (!$this->checkNameLength($node->args[0]->value->value)) { |
||
| 94 | $this->errors[] = [ |
||
| 95 | 'line' => $node->getLine(), |
||
| 96 | 'disallowedToken' => $node->args[0]->value->value, |
||
| 97 | 'reason' => sprintf( |
||
| 98 | 'Column name is too long on table `%s` (max. 27)', |
||
| 99 | $this->tableVariableNames[$node->var->name] |
||
| 100 | ), |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | // On autoincrement the max length of the table name is 21 instead of 27 |
||
| 105 | if (isset($node->args[2]) && $node->args[2]->value instanceof Node\Expr\Array_) { |
||
| 106 | /** @var Node\Expr\Array_ $options */ |
||
| 107 | $options = $node->args[2]->value; |
||
| 108 | if ($this->checkColumnForAutoincrement($options)) { |
||
| 109 | if (!$this->checkNameLength($this->tableVariableNames[$node->var->name], true)) { |
||
| 110 | $this->errors[] = [ |
||
| 111 | 'line' => $node->getLine(), |
||
| 112 | 'disallowedToken' => $this->tableVariableNames[$node->var->name], |
||
| 113 | 'reason' => 'Table name is too long because of autoincrement (max. 21)', |
||
| 114 | ]; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } else if ($node->name === 'addIndex' || |
||
| 120 | $node->name === 'addUniqueIndex' || |
||
| 121 | $node->name === 'renameIndex' || |
||
| 122 | $node->name === 'setPrimaryKey') { |
||
| 123 | View Code Duplication | if (isset($node->args[1]) && $node->args[1]->value instanceof Node\Scalar\String_) { |
|
| 124 | if (!$this->checkNameLength($node->args[1]->value->value)) { |
||
| 125 | $this->errors[] = [ |
||
| 126 | 'line' => $node->getLine(), |
||
| 127 | 'disallowedToken' => $node->args[1]->value->value, |
||
| 128 | 'reason' => sprintf( |
||
| 129 | 'Index name is too long on table `%s` (max. 27)', |
||
| 130 | $this->tableVariableNames[$node->var->name] |
||
| 131 | ), |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } else if ($node->name === 'addForeignKeyConstraint') { |
||
| 136 | View Code Duplication | if (isset($node->args[4]) && $node->args[4]->value instanceof Node\Scalar\String_) { |
|
| 137 | if (!$this->checkNameLength($node->args[4]->value->value)) { |
||
| 138 | $this->errors[] = [ |
||
| 139 | 'line' => $node->getLine(), |
||
| 140 | 'disallowedToken' => $node->args[4]->value->value, |
||
| 141 | 'reason' => sprintf( |
||
| 142 | 'Constraint name is too long on table `%s` (max. 27)', |
||
| 143 | $this->tableVariableNames[$node->var->name] |
||
| 144 | ), |
||
| 145 | ]; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | View Code Duplication | } else if ($node->name === 'renameColumn') { |
|
| 149 | $this->errors[] = [ |
||
| 150 | 'line' => $node->getLine(), |
||
| 151 | 'disallowedToken' => 'Deprecated method', |
||
| 152 | 'reason' => sprintf( |
||
| 153 | '`$%s->renameColumn()` must not be used', |
||
| 154 | $node->var->name |
||
| 155 | ), |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Find the schema |
||
| 161 | */ |
||
| 162 | } else if ($node instanceof Node\Expr\Assign && |
||
| 163 | $node->expr instanceof Node\Expr\FuncCall && |
||
| 164 | $node->var instanceof Node\Expr\Variable && |
||
| 165 | $node->expr->name instanceof Node\Expr\Variable && |
||
| 166 | $node->expr->name->name === 'schemaClosure') { |
||
| 167 | // E.g. $schema = $schemaClosure(); |
||
| 168 | $this->schemaVariableName = $node->var->name; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 202 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.