| Total Complexity | 42 |
| Total Lines | 234 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TestingClass 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 TestingClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class TestingClass extends Testing |
||
| 21 | { |
||
| 22 | public $caption; |
||
| 23 | protected static $property; |
||
| 24 | protected static $required = false; |
||
| 25 | public $expect; |
||
| 26 | |||
| 27 | const RETURN_STRING = 'string'; |
||
| 28 | const RETURN_INTERFACE = 'interface'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * [['name'], 'return' => 'array|string|interface'] |
||
| 32 | * [['name'], 'return' => 'interface', 'value'=>''] |
||
| 33 | * |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | protected static function methodRules() |
||
| 38 | { |
||
| 39 | return []; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $result |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | protected function saveResult($result) |
||
| 47 | { |
||
| 48 | if (\Yii::$app->cache) { |
||
| 49 | \Yii::$app->cache->set([$this->method, self::getPropertyClass()], $result); |
||
| 50 | } |
||
| 51 | return $result; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return mixed|null |
||
| 56 | */ |
||
| 57 | protected function getSavedResult() |
||
| 58 | { |
||
| 59 | if (\Yii::$app->cache) { |
||
| 60 | return \Yii::$app->cache->get([$this->method, self::getPropertyClass()]); |
||
| 61 | } |
||
| 62 | return null; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $params |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | protected static function getParams($params) |
||
| 70 | { |
||
| 71 | $values = []; |
||
| 72 | foreach ($params as $param) { |
||
| 73 | $value = null; |
||
| 74 | $cml = new CommerceML(); |
||
| 75 | $cml->loadImportXml(\Yii::getAlias('@vendor/carono/yii2-1c-exchange/files/xml/import.xml')); |
||
| 76 | $cml->loadOffersXml(\Yii::getAlias('@vendor/carono/yii2-1c-exchange/files/xml/offers.xml')); |
||
| 77 | if ($param instanceof \Closure) { |
||
| 78 | $values[] = call_user_func($param); |
||
| 79 | } elseif (StringHelper::startsWith($param, 'cml.')) { |
||
| 80 | $value = ArrayHelper::getValue($cml, substr($param, 4)); |
||
| 81 | } else { |
||
| 82 | $value = $param; |
||
| 83 | } |
||
| 84 | $values[] = $value; |
||
| 85 | } |
||
| 86 | return $values; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return ActiveRecord|mixed |
||
| 91 | */ |
||
| 92 | protected static function getPropertyClass() |
||
| 93 | { |
||
| 94 | return self::module()->{static::$property}; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param $method |
||
| 99 | * @param $params |
||
| 100 | * @return mixed|null |
||
| 101 | */ |
||
| 102 | protected static function getMethodResult($method, $params) |
||
| 103 | { |
||
| 104 | $class = self::getPropertyClass(); |
||
| 105 | $methodResult = null; |
||
| 106 | if (method_exists($class, $method)) { |
||
| 107 | $reflectionMethod = new \ReflectionMethod($class, $method); |
||
| 108 | $params = self::getParams($params); |
||
| 109 | try { |
||
| 110 | if ($reflectionMethod->isStatic()) { |
||
| 111 | $methodResult = call_user_func_array("$class::$method", $params); |
||
| 112 | } elseif (!$context = static::getContext()) { |
||
| 113 | return null; |
||
| 114 | } else { |
||
| 115 | $methodResult = call_user_func_array([$context, $method], $params); |
||
| 116 | } |
||
| 117 | } catch (\Exception $e) { |
||
| 118 | return null; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | return $methodResult; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Testing $test |
||
| 126 | * @param $method |
||
| 127 | * @param $rule |
||
| 128 | */ |
||
| 129 | private static function validateMethodRule($test, $method, $rule) |
||
| 130 | { |
||
| 131 | |||
| 132 | } |
||
| 133 | |||
| 134 | public static function findAll() |
||
| 135 | { |
||
| 136 | $result = parent::findAll(); |
||
| 137 | foreach (static::methodRules() as $method => $rule) { |
||
| 138 | $test = new static(); |
||
| 139 | $test->name = "Результат '$method'"; |
||
| 140 | $test->method = $method; |
||
| 141 | $test->expect = ArrayHelper::getValue($rule, 'return', false) ?: 'VOID'; |
||
| 142 | $result[] = $test; |
||
| 143 | } |
||
| 144 | return $result; |
||
| 145 | } |
||
| 146 | |||
| 147 | protected static function getContext() |
||
| 148 | { |
||
| 149 | $class = \Yii::$app->controller->module->{static::$property}; |
||
| 150 | return InterfaceTest::findByClass($class)->getModel(); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function testPropertyIsSet() |
||
| 154 | { |
||
| 155 | $property = static::$property; |
||
| 156 | $test = new static(); |
||
| 157 | $test->name = ModuleHelper::getModuleNameByClass() . "->{$property}"; |
||
| 158 | if (!self::module()->{$property}) { |
||
| 159 | $test->result = false; |
||
| 160 | $test->comment = "Необходимо прописать '$property' в модуле '" . ModuleHelper::getModuleNameByClass() . "'"; |
||
| 161 | } |
||
| 162 | return $test; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function hasResult() |
||
| 166 | { |
||
| 167 | return $this->_result; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getResult($force = false) |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | public function prepareResult() |
||
| 183 | { |
||
| 184 | if ($method = $this->method) { |
||
| 185 | $methodName = "getResult" . ucfirst($method); |
||
| 186 | if (method_exists($this, $methodName)) { |
||
| 187 | $result = call_user_func([$this, $methodName]); |
||
| 188 | return $this->_result = $result; |
||
| 189 | } else { |
||
| 190 | $params = ArrayHelper::getValue(static::methodRules(), $method . '.params', []); |
||
| 191 | return $this->_result = static::getMethodResult($method, $params); |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | return null; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | public static function testImplementsClass() |
||
| 199 | { |
||
| 200 | $property = static::$property; |
||
| 201 | $test = new static(); |
||
| 202 | $test->name = "Реализация интерфейсов $property (" . self::module()->{$property} . ")"; |
||
| 203 | $propertyValue = self::module()->{$property}; |
||
| 204 | $propertyDoc = ModuleHelper::getPhpDocInterfaceProperty($property); |
||
| 205 | $implements = ClassHelper::getImplementedMethods($propertyValue, $propertyDoc); |
||
| 206 | $implements = array_filter($implements, function ($data) { |
||
| 207 | return !$data; |
||
| 208 | }); |
||
| 209 | if (!empty($implements)) { |
||
| 210 | $comment = []; |
||
| 211 | foreach ($implements as $class => $value) { |
||
| 212 | $comment[] = $class; |
||
| 213 | $test->addError('', 'Need implement: ' . $class); |
||
| 214 | } |
||
| 215 | $test->comment = "Не реализованы:<br>" . join("<br>", $comment); |
||
| 216 | } |
||
| 217 | return $test; |
||
| 218 | } |
||
| 219 | |||
| 220 | public static function getMethodRule($method) |
||
| 221 | { |
||
| 222 | return ArrayHelper::getValue(static::methodRules(), $method, []); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function isAutoTest() |
||
| 226 | { |
||
| 227 | return ArrayHelper::getValue(self::getMethodRule($this->method), 'auto') == true; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function validateMethod() |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testing() |
||
| 236 | { |
||
| 237 | if (!$rule = self::getMethodRule($this->method)) { |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.