Complex classes like PolicyTree 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 PolicyTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class PolicyTree |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Root node at depth zero. |
||
| 15 | * |
||
| 16 | * @var null|PolicyNode |
||
| 17 | */ |
||
| 18 | protected $_root; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param PolicyNode $root Initial root node |
||
| 24 | */ |
||
| 25 | 49 | public function __construct(PolicyNode $root) |
|
| 26 | { |
||
| 27 | 49 | $this->_root = $root; |
|
| 28 | 49 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Process policy information from the certificate. |
||
| 32 | * |
||
| 33 | * Certificate policies extension must be present. |
||
| 34 | * |
||
| 35 | * @param ValidatorState $state |
||
| 36 | * @param Certificate $cert |
||
| 37 | * |
||
| 38 | * @return ValidatorState |
||
| 39 | 14 | */ |
|
| 40 | public function processPolicies(ValidatorState $state, |
||
| 41 | 14 | Certificate $cert): ValidatorState |
|
| 42 | 14 | { |
|
| 43 | 14 | $policies = $cert->tbsCertificate()->extensions()->certificatePolicies(); |
|
| 44 | 14 | $tree = clone $this; |
|
| 45 | // (d.1) for each policy P not equal to anyPolicy |
||
| 46 | 14 | foreach ($policies as $policy) { |
|
| 47 | 14 | if ($policy->isAnyPolicy()) { |
|
| 48 | 6 | $tree->_processAnyPolicy($policy, $cert, $state); |
|
| 49 | } else { |
||
| 50 | 14 | $tree->_processPolicy($policy, $state); |
|
| 51 | } |
||
| 52 | } |
||
| 53 | // if whole tree is pruned |
||
| 54 | 14 | if (!$tree->_pruneTree($state->index() - 1)) { |
|
| 55 | 1 | return $state->withoutValidPolicyTree(); |
|
| 56 | } |
||
| 57 | 14 | return $state->withValidPolicyTree($tree); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Process policy mappings from the certificate. |
||
| 62 | * |
||
| 63 | * @param ValidatorState $state |
||
| 64 | * @param Certificate $cert |
||
| 65 | * |
||
| 66 | * @return ValidatorState |
||
| 67 | 3 | */ |
|
| 68 | public function processMappings(ValidatorState $state, |
||
| 69 | 3 | Certificate $cert): ValidatorState |
|
| 70 | 3 | { |
|
| 71 | 2 | $tree = clone $this; |
|
| 72 | 1 | if ($state->policyMapping() > 0) { |
|
| 73 | 1 | $tree->_applyMappings($cert, $state); |
|
| 74 | } elseif (0 === $state->policyMapping()) { |
||
| 75 | $tree->_deleteMappings($cert, $state); |
||
| 76 | 3 | } |
|
| 77 | 1 | // if whole tree is pruned |
|
| 78 | if (!$tree->_root) { |
||
| 79 | 2 | return $state->withoutValidPolicyTree(); |
|
| 80 | } |
||
| 81 | return $state->withValidPolicyTree($tree); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g. |
||
| 86 | * |
||
| 87 | * @param ValidatorState $state |
||
| 88 | * @param array $policies |
||
| 89 | 6 | * |
|
| 90 | * @return ValidatorState |
||
| 91 | 6 | */ |
|
| 92 | 6 | public function calculateIntersection(ValidatorState $state, |
|
| 93 | array $policies): ValidatorState |
||
| 94 | { |
||
| 95 | $tree = clone $this; |
||
| 96 | 6 | $valid_policy_node_set = $tree->_validPolicyNodeSet(); |
|
| 97 | 6 | // 2. If the valid_policy of any node in the valid_policy_node_set |
|
| 98 | 6 | // is not in the user-initial-policy-set and is not anyPolicy, |
|
| 99 | 4 | // delete this node and all its children. |
|
| 100 | $valid_policy_node_set = array_filter($valid_policy_node_set, |
||
| 101 | 5 | function (PolicyNode $node) use ($policies) { |
|
| 102 | 3 | if ($node->isAnyPolicy()) { |
|
| 103 | return true; |
||
| 104 | 3 | } |
|
| 105 | 3 | if (in_array($node->validPolicy(), $policies)) { |
|
| 106 | 6 | return true; |
|
| 107 | } |
||
| 108 | 6 | $node->remove(); |
|
| 109 | 6 | return false; |
|
| 110 | 5 | }); |
|
| 111 | 6 | // array of valid policy OIDs |
|
| 112 | $valid_policy_set = array_map( |
||
| 113 | function (PolicyNode $node) { |
||
| 114 | return $node->validPolicy(); |
||
| 115 | 6 | }, $valid_policy_node_set); |
|
| 116 | 4 | // 3. If the valid_policy_tree includes a node of depth n with |
|
| 117 | // the valid_policy anyPolicy and the user-initial-policy-set |
||
| 118 | // is not any-policy |
||
| 119 | 1 | foreach ($tree->_nodesAtDepth($state->index()) as $node) { |
|
| 120 | if ($node->hasParent() && $node->isAnyPolicy()) { |
||
| 121 | // a. Set P-Q to the qualifier_set in the node of depth n |
||
| 122 | // with valid_policy anyPolicy. |
||
| 123 | $pq = $node->qualifiers(); |
||
| 124 | 1 | // b. For each P-OID in the user-initial-policy-set that is not |
|
| 125 | 1 | // the valid_policy of a node in the valid_policy_node_set, |
|
| 126 | 1 | // create a child node whose parent is the node of depth n-1 |
|
| 127 | // with the valid_policy anyPolicy. |
||
| 128 | $poids = array_diff($policies, $valid_policy_set); |
||
| 129 | foreach ($tree->_nodesAtDepth($state->index() - 1) as $parent) { |
||
| 130 | 1 | if ($parent->isAnyPolicy()) { |
|
| 131 | 1 | // Set the values in the child node as follows: |
|
| 132 | 1 | // set the valid_policy to P-OID, set the qualifier_set |
|
| 133 | // to P-Q, and set the expected_policy_set to {P-OID}. |
||
| 134 | 1 | foreach ($poids as $poid) { |
|
| 135 | $parent->addChild(new PolicyNode($poid, $pq, [$poid])); |
||
| 136 | } |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | 4 | } |
|
| 140 | // c. Delete the node of depth n with the |
||
| 141 | // valid_policy anyPolicy. |
||
| 142 | $node->remove(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | 6 | // 4. If there is a node in the valid_policy_tree of depth n-1 or less |
|
| 146 | 2 | // without any child nodes, delete that node. Repeat this step until |
|
| 147 | // there are no nodes of depth n-1 or less without children. |
||
| 148 | 4 | if (!$tree->_pruneTree($state->index() - 1)) { |
|
| 149 | return $state->withoutValidPolicyTree(); |
||
| 150 | } |
||
| 151 | return $state->withValidPolicyTree($tree); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get policies at given policy tree depth. |
||
| 156 | * |
||
| 157 | 6 | * @param int $i Depth in range 1..n |
|
| 158 | * |
||
| 159 | 6 | * @return PolicyInformation[] |
|
| 160 | 6 | */ |
|
| 161 | 5 | public function policiesAtDepth(int $i): array |
|
| 162 | 5 | { |
|
| 163 | $policies = []; |
||
| 164 | 6 | foreach ($this->_nodesAtDepth($i) as $node) { |
|
| 165 | $policies[] = new PolicyInformation( |
||
| 166 | $node->validPolicy(), ...$node->qualifiers()); |
||
| 167 | } |
||
| 168 | return $policies; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Process single policy information. |
||
| 173 | 12 | * |
|
| 174 | * @param PolicyInformation $policy |
||
| 175 | * @param ValidatorState $state |
||
| 176 | 12 | */ |
|
| 177 | 12 | protected function _processPolicy(PolicyInformation $policy, |
|
| 178 | 12 | ValidatorState $state): void |
|
| 179 | { |
||
| 180 | 12 | $p_oid = $policy->oid(); |
|
| 181 | $i = $state->index(); |
||
| 182 | 12 | $match_count = 0; |
|
| 183 | 6 | // (d.1.i) for each node of depth i-1 in the valid_policy_tree... |
|
| 184 | 6 | foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
| 185 | 12 | // ...where P-OID is in the expected_policy_set |
|
| 186 | if ($node->hasExpectedPolicy($p_oid)) { |
||
| 187 | $node->addChild(new PolicyNode( |
||
| 188 | $p_oid, $policy->qualifiers(), [$p_oid])); |
||
| 189 | 12 | ++$match_count; |
|
| 190 | } |
||
| 191 | } |
||
| 192 | 11 | // (d.1.ii) if there was no match in step (i)... |
|
| 193 | 11 | if (!$match_count) { |
|
| 194 | 11 | // ...and the valid_policy_tree includes a node of depth i-1 with |
|
| 195 | 11 | // the valid_policy anyPolicy |
|
| 196 | 11 | foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
| 197 | if ($node->isAnyPolicy()) { |
||
| 198 | $node->addChild(new PolicyNode( |
||
| 199 | $p_oid, $policy->qualifiers(), [$p_oid])); |
||
| 200 | 12 | } |
|
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Process anyPolicy policy information. |
||
| 207 | * |
||
| 208 | * @param PolicyInformation $policy |
||
| 209 | 6 | * @param Certificate $cert |
|
| 210 | * @param ValidatorState $state |
||
| 211 | */ |
||
| 212 | 6 | protected function _processAnyPolicy(PolicyInformation $policy, |
|
| 213 | Certificate $cert, ValidatorState $state): void |
||
| 214 | { |
||
| 215 | 6 | $i = $state->index(); |
|
| 216 | 6 | // if (a) inhibit_anyPolicy is greater than 0 or |
|
| 217 | 1 | // (b) i<n and the certificate is self-issued |
|
| 218 | if (!($state->inhibitAnyPolicy() > 0 || |
||
| 219 | ($i < $state->pathLength() && $cert->isSelfIssued()))) { |
||
| 220 | 6 | return; |
|
| 221 | } |
||
| 222 | 6 | // for each node in the valid_policy_tree of depth i-1 |
|
| 223 | foreach ($this->_nodesAtDepth($i - 1) as $node) { |
||
| 224 | 6 | // for each value in the expected_policy_set |
|
| 225 | 6 | foreach ($node->expectedPolicies() as $p_oid) { |
|
| 226 | 6 | // that does not appear in a child node |
|
| 227 | 6 | if (!$node->hasChildWithValidPolicy($p_oid)) { |
|
| 228 | $node->addChild(new PolicyNode( |
||
| 229 | $p_oid, $policy->qualifiers(), [$p_oid])); |
||
| 230 | } |
||
| 231 | 6 | } |
|
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Apply policy mappings to the policy tree. |
||
| 237 | * |
||
| 238 | * @param Certificate $cert |
||
| 239 | 2 | * @param ValidatorState $state |
|
| 240 | */ |
||
| 241 | 2 | protected function _applyMappings(Certificate $cert, ValidatorState $state): void |
|
| 242 | 2 | { |
|
| 243 | 2 | $policy_mappings = $cert->tbsCertificate()->extensions()->policyMappings(); |
|
| 244 | // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i... |
||
| 245 | 2 | foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
|
| 246 | 2 | $match_count = 0; |
|
| 247 | 2 | foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
| 248 | // ...where ID-P is the valid_policy |
||
| 249 | 2 | if ($node->validPolicy() === $idp) { |
|
| 250 | // set expected_policy_set to the set of subjectDomainPolicy |
||
| 251 | // values that are specified as equivalent to ID-P by |
||
| 252 | // the policy mappings extension |
||
| 253 | 1 | $node->setExpectedPolicies(...$sdps); |
|
| 254 | 2 | ++$match_count; |
|
| 255 | } |
||
| 256 | } |
||
| 257 | // if no node of depth i in the valid_policy_tree has |
||
| 258 | // a valid_policy of ID-P... |
||
| 259 | 2 | if (!$match_count) { |
|
| 260 | 2 | $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
|
| 261 | } |
||
| 262 | } |
||
| 263 | 2 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1). |
||
| 267 | * |
||
| 268 | * @param Certificate $cert |
||
| 269 | * @param ValidatorState $state |
||
| 270 | * @param string $idp OID of the issuer domain policy |
||
| 271 | * @param array $sdps Array of subject domain policy OIDs |
||
| 272 | */ |
||
| 273 | 2 | protected function _applyAnyPolicyMapping(Certificate $cert, |
|
| 274 | ValidatorState $state, string $idp, array $sdps): void |
||
| 275 | { |
||
| 276 | // (6.1.4. b.1.) ...but there is a node of depth i with |
||
| 277 | // a valid_policy of anyPolicy |
||
| 278 | 2 | foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
| 279 | 2 | if ($node->isAnyPolicy()) { |
|
| 280 | // then generate a child node of the node of depth i-1 |
||
| 281 | // that has a valid_policy of anyPolicy as follows... |
||
| 282 | 2 | foreach ($this->_nodesAtDepth($state->index() - 1) as $subnode) { |
|
| 283 | 2 | if ($subnode->isAnyPolicy()) { |
|
| 284 | // try to fetch qualifiers of anyPolicy certificate policy |
||
| 285 | 2 | try { |
|
| 286 | $qualifiers = $cert->tbsCertificate() |
||
| 287 | 2 | ->extensions()->certificatePolicies() |
|
| 288 | 2 | ->anyPolicy()->qualifiers(); |
|
| 289 | 2 | } catch (\LogicException $e) { |
|
| 290 | 2 | // if there's no policies or no qualifiers |
|
| 291 | 1 | $qualifiers = []; |
|
| 292 | 1 | } |
|
| 293 | $subnode->addChild(new PolicyNode($idp, $qualifiers, $sdps)); |
||
| 294 | // bail after first anyPolicy has been processed |
||
| 295 | 2 | break; |
|
| 296 | 2 | } |
|
| 297 | } |
||
| 298 | 2 | // bail after first anyPolicy has been processed |
|
| 299 | break; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | 2 | } |
|
| 303 | |||
| 304 | /** |
||
| 305 | 2 | * Delete nodes as specified in 6.1.4 (b)(2). |
|
| 306 | * |
||
| 307 | * @param Certificate $cert |
||
| 308 | * @param ValidatorState $state |
||
| 309 | */ |
||
| 310 | protected function _deleteMappings(Certificate $cert, |
||
| 323 | 1 | } |
|
| 324 | |||
| 325 | /** |
||
| 326 | 1 | * Prune tree starting from given depth. |
|
| 327 | 1 | * |
|
| 328 | * @param int $depth |
||
| 329 | * |
||
| 330 | * @return int The number of nodes left in a tree |
||
| 331 | */ |
||
| 332 | protected function _pruneTree(int $depth): int |
||
| 333 | { |
||
| 334 | if (!$this->_root) { |
||
| 335 | 14 | return 0; |
|
| 336 | } |
||
| 337 | 14 | for ($i = $depth; $i > 0; --$i) { |
|
| 338 | 11 | foreach ($this->_nodesAtDepth($i) as $node) { |
|
| 339 | 11 | if (!count($node)) { |
|
| 340 | 11 | $node->remove(); |
|
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | // if root has no children left |
||
| 345 | 14 | if (!count($this->_root)) { |
|
| 346 | 4 | $this->_root = null; |
|
| 347 | 4 | return 0; |
|
| 348 | } |
||
| 349 | 14 | return $this->_root->nodeCount(); |
|
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get all nodes at given depth. |
||
| 354 | * |
||
| 355 | * @param int $i |
||
| 356 | * |
||
| 357 | * @return PolicyNode[] |
||
| 358 | 16 | */ |
|
| 359 | protected function _nodesAtDepth(int $i): array |
||
| 360 | 16 | { |
|
| 361 | 1 | if (!$this->_root) { |
|
| 362 | return []; |
||
| 363 | 15 | } |
|
| 364 | 15 | $depth = 0; |
|
| 365 | 15 | $nodes = [$this->_root]; |
|
| 366 | 12 | while ($depth < $i) { |
|
| 367 | 12 | $nodes = self::_gatherChildren(...$nodes); |
|
| 368 | 2 | if (!count($nodes)) { |
|
| 369 | break; |
||
| 370 | 12 | } |
|
| 371 | ++$depth; |
||
| 372 | 15 | } |
|
| 373 | return $nodes; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1. |
||
| 378 | * |
||
| 379 | * @return PolicyNode[] |
||
| 380 | 7 | */ |
|
| 381 | protected function _validPolicyNodeSet(): array |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Gather all children of given nodes to a flattened array. |
||
| 409 | * |
||
| 410 | * @param PolicyNode ...$nodes |
||
| 411 | * |
||
| 412 | 12 | * @return PolicyNode[] |
|
| 413 | */ |
||
| 414 | 12 | private static function _gatherChildren(PolicyNode ...$nodes): array |
|
| 421 | } |
||
| 422 | } |
||
| 423 |