umbrellio /
laravel-ltree
| 1 | <?php |
||||||||||
| 2 | |||||||||||
| 3 | declare(strict_types=1); |
||||||||||
| 4 | |||||||||||
| 5 | namespace Umbrellio\LTree\Helpers; |
||||||||||
| 6 | |||||||||||
| 7 | use Illuminate\Database\Eloquent\Model; |
||||||||||
| 8 | use Illuminate\Support\Facades\DB; |
||||||||||
| 9 | use Umbrellio\LTree\Interfaces\LTreeInterface; |
||||||||||
| 10 | use Umbrellio\LTree\Interfaces\LTreeModelInterface; |
||||||||||
| 11 | use Umbrellio\LTree\Types\LTreeType; |
||||||||||
| 12 | |||||||||||
| 13 | class LTreeHelper |
||||||||||
| 14 | { |
||||||||||
| 15 | /** |
||||||||||
| 16 | * @param LTreeInterface|Model $model |
||||||||||
| 17 | */ |
||||||||||
| 18 | 3 | public function buildPath($model): void |
|||||||||
| 19 | { |
||||||||||
| 20 | 3 | $pathValue = []; |
|||||||||
| 21 | 3 | if ($model->getLtreeParentId()) { |
|||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
The expression
$model->getLtreeParentId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||||||||||
| 22 | 1 | $parent = $model->ltreeParent; |
|||||||||
| 23 | 1 | $pathValue = array_merge($pathValue, $parent->getLtreePath()); |
|||||||||
| 24 | } |
||||||||||
| 25 | 3 | $pathValue[] = $model->getKey(); |
|||||||||
|
0 ignored issues
–
show
The method
getKey() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getKey() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 26 | 3 | DB::statement(sprintf( |
|||||||||
| 27 | 3 | "UPDATE %s SET %s = text2ltree('%s') WHERE %s = %s", |
|||||||||
| 28 | 3 | $model->getTable(), |
|||||||||
|
0 ignored issues
–
show
The method
getTable() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Illuminate\Database\Eloquent\Relations\Pivot.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getTable() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. Loading history...
|
|||||||||||
| 29 | 3 | $model->getLtreePathColumn(), |
|||||||||
|
0 ignored issues
–
show
The method
getLtreePathColumn() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 30 | 3 | implode(LTreeType::TYPE_SEPARATE, $pathValue), |
|||||||||
| 31 | 3 | $model->getKeyName(), |
|||||||||
|
0 ignored issues
–
show
The method
getKeyName() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getKeyName() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 32 | 3 | $model->getKey() |
|||||||||
| 33 | 3 | )); |
|||||||||
| 34 | 3 | $model->refresh(); |
|||||||||
|
0 ignored issues
–
show
The method
refresh() does not exist on Illuminate\Database\Eloquent\Model.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. Loading history...
The method
refresh() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. Loading history...
|
|||||||||||
| 35 | } |
||||||||||
| 36 | |||||||||||
| 37 | /** |
||||||||||
| 38 | * @param LTreeInterface|Model $model |
||||||||||
| 39 | * @param LTreeInterface|Model|null $to |
||||||||||
| 40 | */ |
||||||||||
| 41 | 2 | public function moveNode($model, $to = null, array $columns = []): void |
|||||||||
| 42 | { |
||||||||||
| 43 | 2 | $pathName = $model->getLtreePathColumn(); |
|||||||||
| 44 | 2 | $oldPath = $model->getLtreePath(LTreeModelInterface::AS_STRING); |
|||||||||
|
0 ignored issues
–
show
The method
getLtreePath() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 45 | 2 | $newPath = $to ? $to->getLtreePath(LTreeModelInterface::AS_STRING) : ''; |
|||||||||
| 46 | 2 | $expressions = static::wrapExpressions($columns); |
|||||||||
|
0 ignored issues
–
show
The method
Umbrellio\LTree\Helpers\...lper::wrapExpressions() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 47 | 2 | $expressions[] = " |
|||||||||
| 48 | 2 | \"${pathName}\" = (text2ltree('${newPath}') || subpath(\"${pathName}\", (nlevel(text2ltree('${oldPath}')) - 1))) |
|||||||||
| 49 | 2 | "; |
|||||||||
| 50 | |||||||||||
| 51 | 2 | DB::statement(sprintf( |
|||||||||
| 52 | 2 | "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true", |
|||||||||
| 53 | 2 | $model->getTable(), |
|||||||||
| 54 | 2 | implode(', ', $expressions), |
|||||||||
| 55 | 2 | $pathName, |
|||||||||
| 56 | 2 | $oldPath |
|||||||||
| 57 | 2 | )); |
|||||||||
| 58 | 2 | $model->refresh(); |
|||||||||
| 59 | } |
||||||||||
| 60 | |||||||||||
| 61 | /** |
||||||||||
| 62 | * @param LTreeInterface|Model $model |
||||||||||
| 63 | */ |
||||||||||
| 64 | 1 | public function dropDescendants($model, array $columns = []): void |
|||||||||
| 65 | { |
||||||||||
| 66 | 1 | $sql = sprintf( |
|||||||||
| 67 | 1 | "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true", |
|||||||||
| 68 | 1 | $model->getTable(), |
|||||||||
| 69 | 1 | implode(', ', static::wrapExpressions($columns)), |
|||||||||
|
0 ignored issues
–
show
The method
Umbrellio\LTree\Helpers\...lper::wrapExpressions() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||||
| 70 | 1 | $model->getLtreePathColumn(), |
|||||||||
| 71 | 1 | $model->getLtreePath(LTreeModelInterface::AS_STRING) |
|||||||||
| 72 | 1 | ); |
|||||||||
| 73 | 1 | DB::statement($sql); |
|||||||||
| 74 | 1 | $model->refresh(); |
|||||||||
| 75 | } |
||||||||||
| 76 | |||||||||||
| 77 | 3 | private function wrapExpressions(array $columns): array |
|||||||||
| 78 | { |
||||||||||
| 79 | 3 | $expressions = []; |
|||||||||
| 80 | 3 | foreach ($columns as $column => $value) { |
|||||||||
| 81 | switch (true) { |
||||||||||
| 82 | case $value === null: |
||||||||||
| 83 | 1 | $expressions[] = sprintf('%s = null', (string) $column); |
|||||||||
| 84 | 1 | break; |
|||||||||
| 85 | 3 | case is_string($value): |
|||||||||
| 86 | 3 | $expressions[] = sprintf("%s = '%s'", (string) $column, (string) $value); |
|||||||||
| 87 | 3 | break; |
|||||||||
| 88 | default: |
||||||||||
| 89 | 1 | $expressions[] = sprintf('%s = %s', (string) $column, (string) $value); |
|||||||||
| 90 | } |
||||||||||
| 91 | } |
||||||||||
| 92 | 3 | return $expressions; |
|||||||||
| 93 | } |
||||||||||
| 94 | } |
||||||||||
| 95 |