Completed
Push — master ( ca5656...60398b )
by Morris
32:43 queued 16:28
created

MigrationSchemaChecker::enterNode()   D

Complexity

Conditions 49
Paths 27

Size

Total Lines 132
Code Lines 85

Duplication

Lines 44
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 49
eloc 85
nc 27
nop 1
dl 44
loc 132
rs 4.1818
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\App\CodeChecker;
25
26
use PhpParser\Node;
27
use PhpParser\Node\Name;
28
use PhpParser\NodeVisitorAbstract;
29
30
class MigrationSchemaChecker extends NodeVisitorAbstract {
31
32
	/** @var string */
33
	protected $schemaVariableName = null;
34
	/** @var array */
35
	protected $tableVariableNames = [];
36
	/** @var array */
37
	public $errors = [];
38
39
	public function enterNode(Node $node) {
40
		/**
41
		 * Check tables
42
		 */
43
		if ($this->schemaVariableName !== null &&
44
			 $node instanceof Node\Expr\Assign &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Assign does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
45
			 $node->var instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
			 $node->expr instanceof Node\Expr\MethodCall &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\MethodCall does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
			 $node->expr->var instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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 &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\MethodCall does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
69
			 $node->var instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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 &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\MethodCall does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
			 $node->var instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Array_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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 &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Assign does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
163
			 $node->expr instanceof Node\Expr\FuncCall &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\FuncCall does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
164
			 $node->var instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
165
			 $node->expr->name instanceof Node\Expr\Variable &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
166
			 $node->expr->name->name === 'schemaClosure') {
167
			// E.g. $schema = $schemaClosure();
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
			$this->schemaVariableName = $node->var->name;
169
		}
170
	}
171
172
	protected function checkNameLength($tableName, $hasAutoincrement = false) {
173
		if ($hasAutoincrement) {
174
			return strlen($tableName) <= 21;
175
		}
176
		return strlen($tableName) <= 27;
177
	}
178
179
	/**
180
	 * @param Node\Expr\Array_ $optionsArray
181
	 * @return bool Whether the column is an autoincrement column
182
	 */
183
	protected function checkColumnForAutoincrement(Node\Expr\Array_ $optionsArray) {
184
		foreach ($optionsArray->items as $option) {
185
			if ($option->key instanceof Node\Scalar\String_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
186
				if ($option->key->value === 'autoincrement' &&
187
					 $option->value instanceof Node\Expr\ConstFetch) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\ConstFetch does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
188
					/** @var Node\Expr\ConstFetch $const */
189
					$const = $option->value;
190
191
					if ($const->name instanceof Name &&
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Name does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
192
						 $const->name->parts === ['true']) {
193
						return true;
194
					}
195
				}
196
			}
197
		}
198
199
		return false;
200
	}
201
}
202