Complex classes like NegatedAssertionReconciler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 NegatedAssertionReconciler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class NegatedAssertionReconciler extends Reconciler |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @param array<string, array<string, array{Type\Union}>> $template_type_map |
||
| 29 | * @param string[] $suppressed_issues |
||
| 30 | * @param 0|1|2 $failed_reconciliation |
||
|
|
|||
| 31 | * |
||
| 32 | * @return Type\Union |
||
| 33 | */ |
||
| 34 | public static function reconcile( |
||
| 35 | StatementsAnalyzer $statements_analyzer, |
||
| 36 | string $assertion, |
||
| 37 | bool $is_strict_equality, |
||
| 38 | bool $is_loose_equality, |
||
| 39 | Type\Union $existing_var_type, |
||
| 40 | array $template_type_map, |
||
| 41 | string $old_var_type_string, |
||
| 42 | ?string $key, |
||
| 43 | ?CodeLocation $code_location, |
||
| 44 | array $suppressed_issues, |
||
| 45 | int &$failed_reconciliation |
||
| 46 | ) { |
||
| 47 | $is_equality = $is_strict_equality || $is_loose_equality; |
||
| 48 | |||
| 49 | // this is a specific value comparison type that cannot be negated |
||
| 50 | if ($is_equality && $bracket_pos = strpos($assertion, '(')) { |
||
| 51 | if ($existing_var_type->hasMixed()) { |
||
| 52 | return $existing_var_type; |
||
| 53 | } |
||
| 54 | |||
| 55 | return self::handleLiteralNegatedEquality( |
||
| 56 | $statements_analyzer, |
||
| 57 | $assertion, |
||
| 58 | $bracket_pos, |
||
| 59 | $existing_var_type, |
||
| 60 | $old_var_type_string, |
||
| 61 | $key, |
||
| 62 | $code_location, |
||
| 63 | $suppressed_issues, |
||
| 64 | $is_strict_equality |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!$is_equality) { |
||
| 69 | if ($assertion === 'isset') { |
||
| 70 | if ($existing_var_type->possibly_undefined) { |
||
| 71 | return Type::getEmpty(); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (!$existing_var_type->isNullable() |
||
| 75 | && $key |
||
| 76 | && strpos($key, '[') === false |
||
| 77 | && $key !== '$_SESSION' |
||
| 78 | ) { |
||
| 79 | foreach ($existing_var_type->getAtomicTypes() as $atomic) { |
||
| 80 | if (!$existing_var_type->hasMixed() |
||
| 81 | || $atomic instanceof Type\Atomic\TNonEmptyMixed |
||
| 82 | ) { |
||
| 83 | $failed_reconciliation = 2; |
||
| 84 | |||
| 85 | if ($code_location) { |
||
| 86 | if ($existing_var_type->from_docblock) { |
||
| 87 | if (IssueBuffer::accepts( |
||
| 88 | new DocblockTypeContradiction( |
||
| 89 | 'Cannot resolve types for ' . $key . ' with docblock-defined type ' |
||
| 90 | . $existing_var_type . ' and !isset assertion', |
||
| 91 | $code_location |
||
| 92 | ), |
||
| 93 | $suppressed_issues |
||
| 94 | )) { |
||
| 95 | // fall through |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | if (IssueBuffer::accepts( |
||
| 99 | new TypeDoesNotContainType( |
||
| 100 | 'Cannot resolve types for ' . $key . ' with type ' |
||
| 101 | . $existing_var_type . ' and !isset assertion', |
||
| 102 | $code_location |
||
| 103 | ), |
||
| 104 | $suppressed_issues |
||
| 105 | )) { |
||
| 106 | // fall through |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return $existing_var_type->from_docblock |
||
| 112 | ? Type::getNull() |
||
| 113 | : Type::getEmpty(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return Type::getNull(); |
||
| 119 | } elseif ($assertion === 'array-key-exists') { |
||
| 120 | return Type::getEmpty(); |
||
| 121 | } elseif (substr($assertion, 0, 9) === 'in-array-') { |
||
| 122 | return $existing_var_type; |
||
| 123 | } elseif (substr($assertion, 0, 14) === 'has-array-key-') { |
||
| 124 | return $existing_var_type; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $existing_var_atomic_types = $existing_var_type->getAtomicTypes(); |
||
| 129 | |||
| 130 | if ($assertion === 'false' && isset($existing_var_atomic_types['bool'])) { |
||
| 131 | $existing_var_type->removeType('bool'); |
||
| 132 | $existing_var_type->addType(new TTrue); |
||
| 133 | } elseif ($assertion === 'true' && isset($existing_var_atomic_types['bool'])) { |
||
| 134 | $existing_var_type->removeType('bool'); |
||
| 135 | $existing_var_type->addType(new TFalse); |
||
| 136 | } else { |
||
| 137 | $simple_negated_type = SimpleNegatedAssertionReconciler::reconcile( |
||
| 138 | $assertion, |
||
| 139 | $existing_var_type, |
||
| 140 | $key, |
||
| 141 | $code_location, |
||
| 142 | $suppressed_issues, |
||
| 143 | $failed_reconciliation, |
||
| 144 | $is_equality, |
||
| 145 | $is_strict_equality |
||
| 146 | ); |
||
| 147 | |||
| 148 | if ($simple_negated_type) { |
||
| 149 | return $simple_negated_type; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($assertion === 'iterable' || $assertion === 'countable') { |
||
| 154 | $existing_var_type->removeType('array'); |
||
| 155 | } |
||
| 156 | |||
| 157 | if (!$is_equality |
||
| 158 | && isset($existing_var_atomic_types['int']) |
||
| 159 | && $existing_var_type->from_calculation |
||
| 160 | && ($assertion === 'int' || $assertion === 'float') |
||
| 161 | ) { |
||
| 162 | $existing_var_type->removeType($assertion); |
||
| 163 | |||
| 164 | if ($assertion === 'int') { |
||
| 165 | $existing_var_type->addType(new Type\Atomic\TFloat); |
||
| 166 | } else { |
||
| 167 | $existing_var_type->addType(new Type\Atomic\TInt); |
||
| 168 | } |
||
| 169 | |||
| 170 | $existing_var_type->from_calculation = false; |
||
| 171 | |||
| 172 | return $existing_var_type; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (strtolower($assertion) === 'traversable' |
||
| 176 | && isset($existing_var_atomic_types['iterable']) |
||
| 177 | ) { |
||
| 178 | /** @var Type\Atomic\TIterable */ |
||
| 179 | $iterable = $existing_var_atomic_types['iterable']; |
||
| 180 | $existing_var_type->removeType('iterable'); |
||
| 181 | $existing_var_type->addType(new TArray( |
||
| 182 | [ |
||
| 183 | $iterable->type_params[0]->hasMixed() |
||
| 184 | ? Type::getArrayKey() |
||
| 185 | : clone $iterable->type_params[0], |
||
| 186 | clone $iterable->type_params[1], |
||
| 187 | ] |
||
| 188 | )); |
||
| 189 | } elseif (strtolower($assertion) === 'int' |
||
| 190 | && isset($existing_var_type->getAtomicTypes()['array-key']) |
||
| 191 | ) { |
||
| 192 | $existing_var_type->removeType('array-key'); |
||
| 193 | $existing_var_type->addType(new TString); |
||
| 194 | } elseif (substr($assertion, 0, 9) === 'getclass-') { |
||
| 195 | $assertion = substr($assertion, 9); |
||
| 196 | } elseif (!$is_equality) { |
||
| 197 | $codebase = $statements_analyzer->getCodebase(); |
||
| 198 | |||
| 199 | // if there wasn't a direct hit, go deeper, eliminating subtypes |
||
| 200 | if (!$existing_var_type->removeType($assertion)) { |
||
| 201 | foreach ($existing_var_type->getAtomicTypes() as $part_name => $existing_var_type_part) { |
||
| 202 | if (!$existing_var_type_part->isObjectType() || strpos($assertion, '-')) { |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | |||
| 206 | $new_type_part = Atomic::create($assertion); |
||
| 207 | |||
| 208 | if (!$new_type_part instanceof TNamedObject) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | |||
| 212 | if (TypeAnalyzer::isAtomicContainedBy( |
||
| 213 | $codebase, |
||
| 214 | $existing_var_type_part, |
||
| 215 | $new_type_part, |
||
| 216 | false, |
||
| 217 | false |
||
| 218 | )) { |
||
| 219 | $existing_var_type->removeType($part_name); |
||
| 220 | } elseif (TypeAnalyzer::isAtomicContainedBy( |
||
| 221 | $codebase, |
||
| 222 | $new_type_part, |
||
| 223 | $existing_var_type_part, |
||
| 224 | false, |
||
| 225 | false |
||
| 226 | )) { |
||
| 227 | $existing_var_type->different = true; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($is_strict_equality |
||
| 234 | && $assertion !== 'isset' |
||
| 235 | && ($key !== '$this' |
||
| 236 | || !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)) |
||
| 237 | ) { |
||
| 238 | $assertion = Type::parseString($assertion, null, $template_type_map); |
||
| 239 | |||
| 240 | if ($key |
||
| 241 | && $code_location |
||
| 242 | && !TypeAnalyzer::canExpressionTypesBeIdentical( |
||
| 243 | $statements_analyzer->getCodebase(), |
||
| 244 | $existing_var_type, |
||
| 245 | $assertion |
||
| 246 | ) |
||
| 247 | ) { |
||
| 248 | self::triggerIssueForImpossible( |
||
| 249 | $existing_var_type, |
||
| 250 | $old_var_type_string, |
||
| 251 | $key, |
||
| 252 | '!=' . $assertion, |
||
| 253 | true, |
||
| 254 | $code_location, |
||
| 255 | $suppressed_issues |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | if (empty($existing_var_type->getAtomicTypes())) { |
||
| 261 | if ($key !== '$this' |
||
| 262 | || !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer) |
||
| 263 | ) { |
||
| 264 | if ($key && $code_location && !$is_equality) { |
||
| 265 | self::triggerIssueForImpossible( |
||
| 266 | $existing_var_type, |
||
| 267 | $old_var_type_string, |
||
| 268 | $key, |
||
| 269 | '!' . $assertion, |
||
| 270 | false, |
||
| 271 | $code_location, |
||
| 272 | $suppressed_issues |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | $failed_reconciliation = 2; |
||
| 278 | |||
| 279 | return new Type\Union([new Type\Atomic\TEmptyMixed]); |
||
| 280 | } |
||
| 281 | |||
| 282 | return $existing_var_type; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $assertion |
||
| 287 | * @param int $bracket_pos |
||
| 288 | * @param string $old_var_type_string |
||
| 289 | * @param string|null $key |
||
| 290 | * @param CodeLocation|null $code_location |
||
| 291 | * @param string[] $suppressed_issues |
||
| 292 | * |
||
| 293 | * @return Type\Union |
||
| 294 | */ |
||
| 295 | private static function handleLiteralNegatedEquality( |
||
| 407 | } |
||
| 408 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.