| Total Complexity | 41 |
| Total Lines | 150 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Maphper 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 Maphper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Maphper implements \Countable, \ArrayAccess, \IteratorAggregate { |
||
| 4 | const FIND_EXACT = 0x1; |
||
| 5 | const FIND_LIKE = 0x2; |
||
| 6 | const FIND_STARTS = 0x4; |
||
| 7 | const FIND_ENDS = 0x8; |
||
| 8 | const FIND_BIT = 0x10; |
||
| 9 | const FIND_GREATER = 0x20; |
||
| 10 | const FIND_LESS = 0x40; |
||
| 11 | const FIND_EXPRESSION = 0x80; |
||
| 12 | const FIND_AND = 0x100; |
||
| 13 | const FIND_OR = 0x200; |
||
| 14 | const FIND_NOT = 0x400; |
||
| 15 | const FIND_BETWEEN = 0x800; |
||
| 16 | const FIND_NOCASE = 0x1000; |
||
| 17 | |||
| 18 | private $dataSource; |
||
| 19 | private $relations = []; |
||
| 20 | private $settings = ['filter' => [], 'sort' => null, 'limit' => null, 'offset' => null, 'resultClass' => '\\stdClass']; |
||
| 21 | private $iterator = 0; |
||
| 22 | private $entity; |
||
| 23 | |||
| 24 | public function __construct(DataSource $dataSource, array $settings = [], array $relations = []) { |
||
| 25 | $this->dataSource = $dataSource; |
||
| 26 | $this->settings = array_replace($this->settings, $settings); |
||
| 27 | $this->relations = $relations; |
||
| 28 | $this->entity = new Lib\Entity($this, $this->settings['resultClass'] ?? null); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function addRelation($name, Relation $relation) { |
||
| 33 | } |
||
| 34 | |||
| 35 | public function count($group = null) { |
||
| 37 | } |
||
| 38 | |||
| 39 | //Allow filter(['user' => $user]) where $user is an object instead of |
||
| 40 | //filter(['userId' => $user->id]) |
||
| 41 | private function wrapFilter() { |
||
| 42 | foreach ($this->settings['filter'] as $name => $value) { |
||
| 43 | if (isset($this->relations[$name])) { |
||
| 44 | $filter = $this->relations[$name]->getFilter($value); |
||
| 45 | $this->settings['filter'] = array_merge($this->settings['filter'], $filter); |
||
| 46 | unset($this->settings['filter'][$name]); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | private function getResults() { |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getColumns() { |
||
| 69 | return $this->dataSource->getColumns(); |
||
|
|
|||
| 70 | } |
||
| 71 | |||
| 72 | public function item($n) { |
||
| 73 | $array = $this->getResults(); |
||
| 74 | return isset($array[$n]) ? $array[$n] : null; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getIterator() { |
||
| 78 | return new Iterator($this->getResults(), $this->dataSource->getPrimaryKey()); |
||
| 79 | } |
||
| 80 | |||
| 81 | private function processFilters($value) { |
||
| 87 | } |
||
| 88 | |||
| 89 | public function offsetSet($offset, $valueObj) { |
||
| 90 | if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
||
| 91 | |||
| 92 | //Extract private properties from the object |
||
| 93 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
||
| 94 | $value = $visibilityOverride->getProperties($valueObj); |
||
| 95 | |||
| 96 | $value = $this->processFilters($value); |
||
| 97 | $pk = $this->dataSource->getPrimaryKey(); |
||
| 98 | if ($offset !== null) $value->{$pk[0]} = $offset; |
||
| 99 | $valueCopy = $this->removeRelations(clone $value, $pk); |
||
| 100 | $value = $this->entity->wrap($this->relations, $value); |
||
| 101 | $this->dataSource->save($value); |
||
| 102 | $visibilityOverride->write($value); |
||
| 103 | $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
||
| 104 | $visibilityOverride->write($value); |
||
| 105 | } |
||
| 106 | |||
| 107 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
||
| 108 | foreach ($this->relations as $name => $relation) |
||
| 109 | if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
||
| 110 | |||
| 111 | if (empty($obj->{$pk[0]})) unset($obj->{$pk[0]}); |
||
| 112 | return $obj; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function offsetExists($offset) { |
||
| 116 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
||
| 117 | if (!empty($this->settings['filter'])) { |
||
| 118 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
||
| 119 | return isset($data[0]); |
||
| 120 | } |
||
| 121 | return (bool) $this->dataSource->findById($offset); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function offsetUnset($id) { |
||
| 125 | $this->dataSource->deleteById($id); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function offsetGet($offset) { |
||
| 129 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
||
| 130 | if (!empty($this->settings['filter'])) { |
||
| 131 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
||
| 132 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
||
| 133 | } |
||
| 134 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function __call($method, $args) { |
||
| 138 | if (array_key_exists($method, $this->settings)) { |
||
| 139 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
||
| 140 | if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
||
| 141 | else $maphper->settings[$method] = $args[0]; |
||
| 142 | return $maphper; |
||
| 143 | } |
||
| 144 | else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function findAggregate($function, $field, $group = null) { |
||
| 148 | return $this->dataSource->findAggregate($function, $field, $group, $this->settings['filter']); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function delete() { |
||
| 153 | } |
||
| 154 | } |
||
| 155 |
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.