| Conditions | 13 |
| Paths | 1 |
| Total Lines | 133 |
| Lines | 30 |
| Ratio | 22.56 % |
| 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 |
||
| 11 | public function __invoke() |
||
| 12 | { |
||
| 13 | /* |
||
| 14 | * Run an insert ignore statement against the database. |
||
| 15 | * |
||
| 16 | * @param array $values |
||
| 17 | * @return bool |
||
| 18 | */ |
||
| 19 | Builder::macro('insertIgnore', function (array $values) { |
||
| 20 | return $this->insertOnDuplicateKey($values, null, 'ignore'); |
||
|
|
|||
| 21 | }); |
||
| 22 | |||
| 23 | /* |
||
| 24 | * Run an insert on duplicate key update statement against the database. |
||
| 25 | * |
||
| 26 | * @param array $values |
||
| 27 | * @param array $columnsToUpdate |
||
| 28 | * @param string $type |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | Builder::macro('insertOnDuplicateKey', function ( |
||
| 32 | array $values, |
||
| 33 | array $columnsToUpdate = null, |
||
| 34 | $type = 'on duplicate key' |
||
| 35 | ) { |
||
| 36 | // Since every insert gets treated like a batch insert, we will make sure the |
||
| 37 | // bindings are structured in a way that is convenient for building these |
||
| 38 | // inserts statements by verifying the elements are actually an array. |
||
| 39 | if (empty($values)) { |
||
| 40 | return true; |
||
| 41 | } |
||
| 42 | |||
| 43 | View Code Duplication | if (!\is_array(reset($values))) { |
|
| 44 | $values = [$values]; |
||
| 45 | } |
||
| 46 | |||
| 47 | // Here, we will sort the insert keys for every record so that each insert is |
||
| 48 | // in the same order for the record. We need to make sure this is the case |
||
| 49 | // so there are not any errors or problems when inserting these records. |
||
| 50 | else { |
||
| 51 | foreach ($values as $key => $value) { |
||
| 52 | ksort($value); |
||
| 53 | $values[$key] = $value; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | // Finally, we will run this query against the database connection and return |
||
| 57 | // the results. We will need to also flatten these bindings before running |
||
| 58 | // the query so they are all in one huge, flattened array for execution. |
||
| 59 | $bindings = $this->cleanBindings(Arr::flatten($values, 1)); |
||
| 60 | // Essentially we will force every insert to be treated as a batch insert which |
||
| 61 | // simply makes creating the SQL easier for us since we can utilize the same |
||
| 62 | // basic routine regardless of an amount of records given to us to insert. |
||
| 63 | $table = $this->grammar->wrapTable($this->from); |
||
| 64 | |||
| 65 | $columns = array_keys(reset($values)); |
||
| 66 | |||
| 67 | $columnsString = $this->grammar->columnize($columns); |
||
| 68 | |||
| 69 | // We need to build a list of parameter place-holders of values that are bound |
||
| 70 | // to the query. Each insert should have the exact same amount of parameter |
||
| 71 | // bindings so we will loop through the record and parameterize them all. |
||
| 72 | $parameters = collect($values)->map(function ($record) { |
||
| 73 | return '('.$this->grammar->parameterize($record).')'; |
||
| 74 | })->implode(', '); |
||
| 75 | |||
| 76 | $sql = 'insert '.($type === 'ignore' ? 'ignore ' : '')."into $table ($columnsString) values $parameters"; |
||
| 77 | |||
| 78 | if ($type === 'ignore') { |
||
| 79 | return $this->connection->insert($sql, $bindings); |
||
| 80 | } |
||
| 81 | |||
| 82 | $sql .= ' on duplicate key update '; |
||
| 83 | |||
| 84 | // We will update all the columns specified in $values by default. |
||
| 85 | if ($columnsToUpdate === null) { |
||
| 86 | $columnsToUpdate = $columns; |
||
| 87 | } |
||
| 88 | |||
| 89 | foreach ($columnsToUpdate as $key => $value) { |
||
| 90 | $column = is_int($key) ? $value : $key; |
||
| 91 | $column = $this->grammar->wrap($column); |
||
| 92 | $sql .= "$column = "; |
||
| 93 | if (is_int($key)) { |
||
| 94 | $sql .= "VALUES($column)"; |
||
| 95 | } else { |
||
| 96 | if ($this->grammar->isExpression($value)) { |
||
| 97 | $sql .= $value->getValue(); |
||
| 98 | } else { |
||
| 99 | $sql .= '?'; |
||
| 100 | $bindings[] = $value; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | $sql .= ','; |
||
| 104 | } |
||
| 105 | |||
| 106 | return $this->connection->insert(rtrim($sql, ','), $bindings); |
||
| 107 | }); |
||
| 108 | |||
| 109 | /* |
||
| 110 | * Attach models to the parent ignoring existing associations. |
||
| 111 | * |
||
| 112 | * @param mixed $id |
||
| 113 | * @param array $attributes |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | View Code Duplication | BelongsToMany::macro('attachIgnore', function ($id, array $attributes = [], $touch = true) { |
|
| 117 | $this->newPivotStatement()->insertIgnore($this->formatAttachRecords( |
||
| 118 | $this->parseIds($id), |
||
| 119 | $attributes |
||
| 120 | )); |
||
| 121 | if ($touch) { |
||
| 122 | $this->touchIfTouching(); |
||
| 123 | } |
||
| 124 | }); |
||
| 125 | |||
| 126 | /* |
||
| 127 | * Attach models to the parent updating existing associations. |
||
| 128 | * |
||
| 129 | * @param mixed $id |
||
| 130 | * @param array $attributes |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | View Code Duplication | BelongsToMany::macro('attachOnDuplicateKey', function ($id, array $attributes = [], $touch = true) { |
|
| 134 | $this->newPivotStatement()->insertOnDuplicateKey($this->formatAttachRecords( |
||
| 135 | $this->parseIds($id), |
||
| 136 | $attributes |
||
| 137 | )); |
||
| 138 | |||
| 139 | if ($touch) { |
||
| 140 | $this->touchIfTouching(); |
||
| 141 | } |
||
| 142 | }); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.