| Total Complexity | 57 |
| Total Lines | 253 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SpatialTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SpatialTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | trait SpatialTrait |
||
| 32 | { |
||
| 33 | /* |
||
| 34 | * The attributes that are spatial representations. |
||
| 35 | * To use this Trait, add the following array to the model class |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | * |
||
| 39 | * protected $spatialFields = []; |
||
| 40 | */ |
||
| 41 | |||
| 42 | public $geometries = []; |
||
| 43 | |||
| 44 | protected $stRelations = [ |
||
| 45 | 'Within', |
||
| 46 | 'Crosses', |
||
| 47 | 'Contains', |
||
| 48 | 'Disjoint', |
||
| 49 | 'Equals', |
||
| 50 | 'Intersects', |
||
| 51 | 'Overlaps', |
||
| 52 | 'Touches', |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a new Eloquent query builder for the model. |
||
| 57 | * |
||
| 58 | * @param \Illuminate\Database\Query\Builder $query |
||
| 59 | * @return \LaravelSpatial\Eloquent\Builder |
||
| 60 | */ |
||
| 61 | public function newEloquentBuilder($query) |
||
| 62 | { |
||
| 63 | return new Builder($query); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function setRawAttributes(array $attributes, $sync = false) |
||
| 67 | { |
||
| 68 | $spatial_fields = $this->getSpatialFields(); |
||
| 69 | |||
| 70 | foreach ($attributes as $attribute => &$value) { |
||
| 71 | if (in_array($attribute, $spatial_fields) && is_string($value) && strlen($value) >= 15) { |
||
| 72 | // MySQL adds 4 NULL bytes at the start of the binary |
||
| 73 | if ($this->getConnection() instanceof MySqlConnection && substr($value, 0, 4) == "\0\0\0\0") { |
||
|
1 ignored issue
–
show
|
|||
| 74 | $value = substr($value, 4); |
||
| 75 | } elseif ($this->getConnection() instanceof PostgresConnection) { |
||
| 76 | $value = pack('H*', $value); |
||
| 77 | } |
||
| 78 | |||
| 79 | try { |
||
| 80 | $value = GeoJson::jsonUnserialize(json_decode(geoPHP::load($value, 'wkb')->out('json'))); |
||
| 81 | } catch (\Exception $e) { |
||
| 82 | throw new \Exception("Can't parse WKB {$value}: {$e->getMessage()}", $e->getCode(), $e); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | parent::setRawAttributes($attributes, $sync); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getSpatialFields() |
||
| 91 | { |
||
| 92 | if (property_exists($this, 'spatialFields') && !empty($this->spatialFields)) { |
||
| 93 | return $this->spatialFields; |
||
| 94 | } else { |
||
| 95 | throw new SpatialFieldsNotDefinedException(__CLASS__ . ' has to define $spatialFields'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | protected function toWkt(Geometry $value) |
||
| 100 | { |
||
| 101 | return ($this->getConnection() instanceof PostgresConnection ? 'SRID=4326;' : '') . |
||
| 102 | geoPHP::load(json_decode(json_encode($value->jsonSerialize()), false), 'json')->out('wkt'); |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function performInsert(EloquentBuilder $query, array $options = []) |
||
| 106 | { |
||
| 107 | foreach ($this->attributes as $key => $value) { |
||
| 108 | if ($value instanceof Geometry && $this->isColumnAllowed($key)) { |
||
| 109 | $this->geometries[$key] = $value; // Preserve the geometry objects prior to the insert |
||
| 110 | $this->attributes[$key] = $this->getConnection()->raw("ST_GeomFromText('{$this->toWkt($value)}')"); |
||
|
1 ignored issue
–
show
|
|||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | $insert = parent::performInsert($query, $options); |
||
| 115 | |||
| 116 | foreach ($this->geometries as $key => $value) { |
||
| 117 | $this->attributes[$key] = $value; // Retrieve the geometry objects so they can be used in the model |
||
| 118 | } |
||
| 119 | |||
| 120 | return $insert; // Return the result of the parent insert |
||
| 121 | } |
||
| 122 | |||
| 123 | public function isColumnAllowed($geometryColumn) |
||
| 124 | { |
||
| 125 | if (!in_array($geometryColumn, $this->getSpatialFields())) { |
||
| 126 | throw new SpatialFieldsNotDefinedException(); |
||
| 127 | } |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function scopeDistance($query, $geometryColumn, $geometry, $distance) |
||
| 133 | { |
||
| 134 | if ($this->isColumnAllowed($geometryColumn)) { |
||
| 135 | $geometryColumn .= $this->getConnection() instanceof PostgresConnection ? '::geometry' : ''; |
||
| 136 | $query->whereRaw("ST_Distance({$geometryColumn}, ST_GeomFromText(?)) <= ?", [ |
||
| 137 | $this->toWkt($geometry), |
||
| 138 | $distance, |
||
| 139 | ]); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $query; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $distance) |
||
| 157 | } |
||
| 158 | |||
| 159 | public function scopeDistanceValue($query, $geometryColumn, $geometry) |
||
| 160 | { |
||
| 161 | if ($this->isColumnAllowed($geometryColumn)) { |
||
| 162 | $columns = $query->getQuery()->columns; |
||
| 163 | |||
| 164 | if (!$columns) { |
||
| 165 | $query->select('*'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $geometryColumn .= $this->getConnection() instanceof PostgresConnection ? '::geometry' : ''; |
||
| 169 | $query->selectRaw("ST_Distance({$geometryColumn}, ST_GeomFromText(?)) as distance", [ |
||
| 170 | $this->toWkt($geometry), |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $query; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance) |
||
| 178 | { |
||
| 179 | $distFunc = $this->getConnection() instanceof PostgresConnection ? 'ST_DistanceSphere' : 'ST_Distance_Sphere'; |
||
| 180 | |||
| 181 | if ($this->isColumnAllowed($geometryColumn)) { |
||
| 182 | $geometryColumn .= $this->getConnection() instanceof PostgresConnection ? '::geometry' : ''; |
||
| 183 | $query->whereRaw("{$distFunc}({$geometryColumn}, ST_GeomFromText(?)) <= ?", [ |
||
| 184 | $this->toWkt($geometry), |
||
| 185 | $distance, |
||
| 186 | ]); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $query; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geometry, $distance) |
||
| 206 | } |
||
| 207 | |||
| 208 | public function scopeDistanceSphereValue($query, $geometryColumn, $geometry) |
||
| 209 | { |
||
| 210 | $distFunc = $this->getConnection() instanceof PostgresConnection ? 'ST_DistanceSphere' : 'ST_Distance_Sphere'; |
||
| 211 | |||
| 212 | if ($this->isColumnAllowed($geometryColumn)) { |
||
| 213 | $columns = $query->getQuery()->columns; |
||
| 214 | |||
| 215 | if (!$columns) { |
||
| 216 | $query->select('*'); |
||
| 217 | } |
||
| 218 | |||
| 219 | $geometryColumn .= $this->getConnection() instanceof PostgresConnection ? '::geometry' : ''; |
||
| 220 | $query->selectRaw("{$distFunc}({$geometryColumn}, ST_GeomFromText(?)) as distance", [ |
||
| 221 | $this->toWkt($geometry), |
||
| 222 | ]); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $query; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function scopeComparison($query, $geometryColumn, $geometry, $relationship) |
||
| 229 | { |
||
| 230 | if ($this->isColumnAllowed($geometryColumn)) { |
||
| 231 | $relationship = ucfirst(strtolower($relationship)); |
||
| 232 | |||
| 233 | if (!in_array($relationship, $this->stRelations)) { |
||
| 234 | throw new UnknownSpatialRelationFunction($relationship); |
||
| 235 | } |
||
| 236 | |||
| 237 | $geometryColumn .= $this->getConnection() instanceof PostgresConnection ? '::geometry' : ''; |
||
| 238 | $query->whereRaw("ST_{$relationship}(`{$geometryColumn}`, ST_GeomFromText(?))", [ |
||
| 239 | $this->toWkt($geometry), |
||
| 240 | ]); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $query; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function scopeWithin($query, $geometryColumn, $polygon) |
||
| 249 | } |
||
| 250 | |||
| 251 | public function scopeCrosses($query, $geometryColumn, $geometry) |
||
| 252 | { |
||
| 253 | return $this->scopeComparison($query, $geometryColumn, $geometry, 'crosses'); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function scopeContains($query, $geometryColumn, $geometry) |
||
| 257 | { |
||
| 258 | return $this->scopeComparison($query, $geometryColumn, $geometry, 'contains'); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function scopeDisjoint($query, $geometryColumn, $geometry) |
||
| 262 | { |
||
| 263 | return $this->scopeComparison($query, $geometryColumn, $geometry, 'disjoint'); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function scopeEquals($query, $geometryColumn, $geometry) |
||
| 267 | { |
||
| 268 | return $this->scopeComparison($query, $geometryColumn, $geometry, 'equals'); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function scopeIntersects($query, $geometryColumn, $geometry) |
||
| 274 | } |
||
| 275 | |||
| 276 | public function scopeOverlaps($query, $geometryColumn, $geometry) |
||
| 277 | { |
||
| 279 | } |
||
| 280 | |||
| 281 | public function scopeDoesTouch($query, $geometryColumn, $geometry) |
||
| 282 | { |
||
| 283 | return $this->scopeComparison($query, $geometryColumn, $geometry, 'touches'); |
||
| 284 | } |
||
| 285 | } |
||
| 286 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths