LaravelFreelancerNL /
laravel-arangodb
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaravelFreelancerNL\Aranguent\Schema\Concerns; |
||
| 4 | |||
| 5 | use ArangoDBClient\Exception; |
||
| 6 | use Illuminate\Support\Fluent; |
||
| 7 | |||
| 8 | trait Indexes |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Add a new index command to the blueprint. |
||
| 12 | * |
||
| 13 | * @param string $type |
||
| 14 | * @param string|array|null $columns |
||
| 15 | * @param $name |
||
| 16 | * @param array $indexOptions |
||
| 17 | * @return Fluent |
||
| 18 | */ |
||
| 19 | protected function indexCommand($type = '', $columns = null, $name = null, $indexOptions = []) |
||
| 20 | { |
||
| 21 | if ($type == '') { |
||
| 22 | $type = $this->mapIndexType('persistent'); |
||
| 23 | } |
||
| 24 | |||
| 25 | if ($columns === null) { |
||
| 26 | $columns = end($this->columns); |
||
|
0 ignored issues
–
show
|
|||
| 27 | } |
||
| 28 | |||
| 29 | if (is_string($columns)) { |
||
| 30 | $columns = [$columns]; |
||
| 31 | } |
||
| 32 | |||
| 33 | $indexOptions['name'] = $name ?: $this->createIndexName($type, $columns); |
||
| 34 | |||
| 35 | return $this->addCommand('index', compact('type', 'columns', 'indexOptions')); |
||
|
0 ignored issues
–
show
It seems like
addCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the Loading history...
|
|||
| 36 | } |
||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Specify an index for the table. |
||
| 42 | * |
||
| 43 | * @param string|array $columns |
||
| 44 | * @param string $name |
||
| 45 | * @param string|null $algorithm |
||
| 46 | * @return Fluent |
||
| 47 | */ |
||
| 48 | public function index($columns = null, $name = null, $algorithm = null) |
||
| 49 | { |
||
| 50 | $type = $this->mapIndexType($algorithm); |
||
| 51 | |||
| 52 | return $this->indexCommand($type, $columns); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a hash index for fast exact matching. |
||
| 57 | * |
||
| 58 | * @param null $columns |
||
| 59 | * @param array $indexOptions |
||
| 60 | * @return Fluent |
||
| 61 | */ |
||
| 62 | public function hashIndex($columns = null, $indexOptions = []) |
||
| 63 | { |
||
| 64 | return $this->indexCommand('hash', $columns, $indexOptions); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param null|string $column |
||
| 69 | * @param $name |
||
| 70 | * @param array $indexOptions |
||
| 71 | * @return Fluent |
||
| 72 | */ |
||
| 73 | public function fulltextIndex($column = null, $name = null, $indexOptions = []) |
||
| 74 | { |
||
| 75 | return $this->indexCommand('fulltext', $column, $name, $indexOptions); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Specify a spatial index for the table. |
||
| 80 | * |
||
| 81 | * @param $columns |
||
| 82 | * @param null $name |
||
| 83 | * @param array $indexOptions |
||
| 84 | * @return Fluent |
||
| 85 | */ |
||
| 86 | public function geoIndex($columns, $name = null, $indexOptions = []) |
||
| 87 | { |
||
| 88 | return $this->indexCommand('geo', $columns, $name, $indexOptions); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Specify a spatial index for the table. |
||
| 93 | * @param string|array $columns |
||
| 94 | * @param string $name |
||
| 95 | * @return Fluent |
||
| 96 | */ |
||
| 97 | public function spatialIndex($columns, $name = null) |
||
| 98 | { |
||
| 99 | return $this->geoIndex($columns, $name); |
||
|
0 ignored issues
–
show
It seems like
$name defined by parameter $name on line 97 can also be of type string; however, LaravelFreelancerNL\Aran...rns\Indexes::geoIndex() does only seem to accept null, maybe add an additional type check?
This check looks at variables that have been passed in as parameters and are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. Loading history...
|
|||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param $columns |
||
| 104 | * @param string|null $name |
||
| 105 | * @param array $indexOptions |
||
| 106 | * @return Fluent |
||
| 107 | */ |
||
| 108 | public function skiplistIndex($columns, $name = null, $indexOptions = []) |
||
| 109 | { |
||
| 110 | return $this->indexCommand('skiplist', $columns, $name, $indexOptions); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function persistentIndex($columns, $name = null, $indexOptions = []) |
||
| 114 | { |
||
| 115 | return $this->indexCommand('persistent', $columns, $name, $indexOptions); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Create a TTL index for the table. |
||
| 120 | * |
||
| 121 | * @param $columns |
||
| 122 | * @param null $name |
||
| 123 | * @param array $indexOptions |
||
| 124 | * @return Fluent |
||
| 125 | */ |
||
| 126 | public function ttlIndex($columns, $name = null, $indexOptions = []) |
||
| 127 | { |
||
| 128 | return $this->indexCommand('ttl', $columns, $name, $indexOptions); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Specify a unique index for the table. |
||
| 133 | * |
||
| 134 | * @param string|array $columns |
||
| 135 | * @param string $name |
||
| 136 | * @param string|null $algorithm |
||
| 137 | * @return Fluent |
||
| 138 | */ |
||
| 139 | public function unique($columns = null, $name = null, $algorithm = null) |
||
| 140 | { |
||
| 141 | $type = $this->mapIndexType($algorithm); |
||
| 142 | |||
| 143 | $indexOptions['unique'] = true; |
||
| 144 | |||
| 145 | return $this->indexCommand($type, $columns, $name, $indexOptions); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param $command |
||
| 150 | * @throws Exception |
||
| 151 | */ |
||
| 152 | public function executeIndexCommand($command) |
||
| 153 | { |
||
| 154 | if ($this->connection->pretending()) { |
||
| 155 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
|
0 ignored issues
–
show
The property
connection does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
| 156 | |||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | $options = [ |
||
| 161 | 'type' => $command->type, |
||
| 162 | 'fields' => $command->columns, |
||
| 163 | 'unique' => $command->unique, |
||
| 164 | 'options' => $command->indexOptions |
||
| 165 | ]; |
||
| 166 | |||
| 167 | if (isset($command->indexOptions) && is_array($command->indexOptions)) { |
||
| 168 | $options = array_merge($options, $command->indexOptions); |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->collectionHandler->createIndex($this->table, $options); |
||
|
0 ignored issues
–
show
The property
collectionHandler does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
The property
table does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Indicate that the given index should be dropped. |
||
| 176 | * |
||
| 177 | * @param $name |
||
| 178 | * @return Fluent |
||
| 179 | */ |
||
| 180 | public function dropIndex($name) |
||
| 181 | { |
||
| 182 | $parameters['name'] = 'dropIndex'; |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. Loading history...
|
|||
| 183 | $parameters['index'] = $name; |
||
| 184 | $parameters['explanation'] = "Drop the '".$name."' index on the {$this->table} table."; |
||
| 185 | $parameters['handler'] = 'collection'; |
||
| 186 | |||
| 187 | return $this->addCommand('dropIndex', $parameters); |
||
|
0 ignored issues
–
show
It seems like
addCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the Loading history...
|
|||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Drop the index by first getting all the indexes on the table; then selecting the matching one |
||
| 192 | * by type and columns. |
||
| 193 | * @param $command |
||
| 194 | */ |
||
| 195 | public function executeDropIndexCommand($command) |
||
| 196 | { |
||
| 197 | if ($this->connection->pretending()) { |
||
| 198 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
| 199 | return; |
||
| 200 | } |
||
| 201 | $this->collectionHandler->dropIndex($this->table, $command->index); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string|null $algorithm |
||
| 206 | * @return mixed|string |
||
| 207 | */ |
||
| 208 | protected function mapIndexType($algorithm) |
||
| 209 | { |
||
| 210 | $typeConversion = [ |
||
| 211 | 'HASH' => 'hash', |
||
| 212 | 'BTREE' => 'persistent', |
||
| 213 | 'RTREE' => 'geo', |
||
| 214 | 'TTL' => 'ttl', |
||
| 215 | ]; |
||
| 216 | $algorithm = strtoupper($algorithm); |
||
| 217 | |||
| 218 | return (isset($typeConversion[$algorithm])) ? $typeConversion[$algorithm] : 'persistent'; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Create a default index name for the table. |
||
| 223 | * |
||
| 224 | * @param string $type |
||
| 225 | * @param array $columns |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function createIndexName($type, array $columns) |
||
| 229 | { |
||
| 230 | $index = strtolower($this->prefix.$this->table.'_'.implode('_', $columns).'_'.$type); |
||
|
0 ignored issues
–
show
The property
prefix does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
| 231 | |||
| 232 | return str_replace(['-', '.'], '_', $index); |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: