Complex classes like MissingSubjectTripleNodeSimplifier 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 MissingSubjectTripleNodeSimplifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class MissingSubjectTripleNodeSimplifier implements NodeSimplifier { |
||
| 32 | |||
| 33 | const QUERY_LIMIT = 50; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var NodeSimplifierFactory |
||
| 37 | */ |
||
| 38 | private $nodeSimplifierFactory; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EntityStore |
||
| 42 | */ |
||
| 43 | private $entityStore; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ResourceListNodeParser |
||
| 47 | */ |
||
| 48 | private $resourceListNodeParser; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param NodeSimplifierFactory $nodeSimplifierFactory |
||
| 52 | * @param EntityStore $entityStore |
||
| 53 | * @param ResourceListNodeParser $resourceListNodeParser |
||
| 54 | */ |
||
| 55 | 22 | public function __construct(NodeSimplifierFactory $nodeSimplifierFactory, EntityStore $entityStore, ResourceListNodeParser $resourceListNodeParser) { |
|
| 56 | 22 | $this->nodeSimplifierFactory = $nodeSimplifierFactory; |
|
| 57 | 22 | $this->entityStore = $entityStore; |
|
| 58 | 22 | $this->resourceListNodeParser = $resourceListNodeParser; |
|
| 59 | 22 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @see AbstractNode::isSimplifierFor |
||
| 63 | */ |
||
| 64 | 7 | public function isSimplifierFor(AbstractNode $node) { |
|
| 67 | |||
| 68 | 7 | private function isTripleWithMissingSubject(AbstractNode $node) { |
|
| 72 | |||
| 73 | 5 | private function isTripleWithMissingSubjectOperator(AbstractNode $node) { |
|
| 74 | 5 | if(!($node instanceof IntersectionNode || $node instanceof UnionNode)) { |
|
| 75 | 4 | return false; |
|
| 76 | } |
||
| 77 | |||
| 78 | 2 | foreach($node->getOperands() as $operand) { |
|
| 79 | 2 | if(!$this->isSimplifierFor($operand)) { |
|
| 80 | 1 | return false; |
|
| 81 | } |
||
| 82 | 1 | } |
|
| 83 | |||
| 84 | 1 | return true; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @see NodeSimplifier::doSimplification |
||
| 89 | */ |
||
| 90 | 14 | public function simplify(AbstractNode $node) { |
|
| 91 | try { |
||
| 92 | 14 | $query = $this->buildQueryForNode($node); |
|
| 93 | 14 | } catch(EmptyQueryException $e) { |
|
| 94 | 4 | return new ResourceListNode(); |
|
| 95 | } |
||
| 96 | |||
| 97 | 6 | $entityIds = $this->entityStore->getItemIdForQueryLookup()->getItemIdsForQuery($query, new QueryOptions(self::QUERY_LIMIT, 0)); |
|
| 98 | |||
| 99 | 6 | return $this->formatQueryResult($entityIds); |
|
| 100 | } |
||
| 101 | |||
| 102 | 14 | private function buildQueryForNode(AbstractNode $node) { |
|
| 113 | |||
| 114 | 5 | private function buildQueryForUnion(UnionNode $unionNode) { |
|
| 115 | 5 | $queries = array(); |
|
| 116 | |||
| 117 | 5 | foreach($unionNode->getOperands() as $operandNode) { |
|
| 118 | try { |
||
| 119 | 4 | $queries[] = $this->buildQueryForNode($operandNode); |
|
| 120 | 4 | } catch(EmptyQueryException $e) { |
|
| 121 | //May be ignored: we are in a union |
||
| 122 | } |
||
| 123 | 4 | } |
|
| 124 | |||
| 125 | 4 | switch(count($queries)) { |
|
| 126 | 4 | case 0: |
|
| 127 | 2 | throw new EmptyQueryException(); |
|
| 128 | 2 | case 1: |
|
| 129 | 1 | return reset($queries); |
|
| 130 | 1 | default: |
|
| 131 | 1 | return new Disjunction($queries); |
|
| 132 | 1 | } |
|
| 133 | } |
||
| 134 | |||
| 135 | 3 | private function buildQueryForIntersection(IntersectionNode $intersectionNode) { |
|
| 136 | 3 | $queries = array(); |
|
| 137 | |||
| 138 | 3 | foreach($intersectionNode->getOperands() as $operandNode) { |
|
| 139 | 3 | $queries[] = $this->buildQueryForNode($operandNode); |
|
| 140 | 2 | } |
|
| 141 | |||
| 142 | 2 | switch(count($queries)) { |
|
| 143 | 2 | case 1: |
|
| 144 | 1 | return reset($queries); |
|
| 145 | 1 | default: |
|
| 146 | 1 | return new Conjunction($queries); |
|
| 147 | 1 | } |
|
| 148 | } |
||
| 149 | |||
| 150 | 12 | private function buildQueryForTriple(TripleNode $triple) { |
|
| 151 | 12 | if(!($triple->getSubject()->equals(new MissingNode()))) { |
|
| 152 | 3 | throw new InvalidArgumentException('Triple whose subject is not missing given.'); |
|
| 153 | } |
||
| 154 | |||
| 155 | 9 | $simplifier = $this->nodeSimplifierFactory->newNodeSimplifier(); |
|
| 156 | 9 | $predicate = $simplifier->simplify($triple->getPredicate()); |
|
| 157 | 9 | $object = $simplifier->simplify($triple->getObject()); |
|
| 158 | 9 | if(!($predicate instanceof ResourceListNode && $object instanceof ResourceListNode)) { |
|
| 159 | throw new NodeSimplifierException('Invalid triple'); |
||
| 160 | } |
||
| 161 | |||
| 162 | 9 | $propertyNodes = $this->resourceListNodeParser->parse($predicate, 'wikibase-property'); |
|
| 163 | 9 | $queryParameters = array(); |
|
| 164 | |||
| 165 | 9 | foreach($this->bagsPropertiesPerType($propertyNodes) as $objectType => $propertyNodes) { |
|
| 166 | 7 | $objectNodes = $this->resourceListNodeParser->parse($object, $objectType); |
|
| 167 | |||
| 168 | 7 | foreach($propertyNodes as $propertyNode) { |
|
| 169 | try { |
||
| 170 | 7 | $queryParameters[] = $this->buildQueryForProperty($propertyNode, $objectNodes); |
|
| 171 | 7 | } catch(EmptyQueryException $e) { |
|
| 172 | //May be ignored: we are in a union |
||
| 173 | } |
||
| 174 | 7 | } |
|
| 175 | 9 | } |
|
| 176 | |||
| 177 | 9 | switch(count($queryParameters)) { |
|
| 178 | 9 | case 0: |
|
| 179 | 3 | throw new EmptyQueryException(); |
|
| 180 | 6 | case 1: |
|
| 181 | 4 | return reset($queryParameters); |
|
| 182 | 2 | default: |
|
| 183 | 2 | return new Disjunction($queryParameters); |
|
| 184 | 2 | } |
|
| 185 | } |
||
| 186 | |||
| 187 | 9 | private function bagsPropertiesPerType($propertyNodes) { |
|
| 188 | 9 | $propertyNodesPerType = array(); |
|
| 189 | |||
| 190 | /** @var WikibaseResourceNode $propertyNode */ |
||
| 191 | 9 | foreach($propertyNodes as $propertyNode) { |
|
| 192 | 7 | $objectType = $this->entityStore->getPropertyLookup()->getPropertyForId( |
|
| 193 | 7 | $propertyNode->getDataValue()->getEntityId() |
|
| 194 | 7 | )->getDataTypeId(); |
|
| 195 | 7 | $propertyNodesPerType[$objectType][] = $propertyNode; |
|
| 196 | 9 | } |
|
| 197 | |||
| 198 | 9 | return $propertyNodesPerType; |
|
| 199 | } |
||
| 200 | |||
| 201 | 7 | private function buildQueryForProperty(WikibaseResourceNode $predicate, ResourceListNode $objectList) { |
|
| 202 | 7 | return new SomeProperty( |
|
| 203 | 7 | new EntityIdValue($predicate->getDataValue()->getEntityId()), |
|
|
|
|||
| 204 | 7 | $this->buildValueDescriptionsForObjects($objectList) |
|
| 205 | 6 | ); |
|
| 206 | } |
||
| 207 | |||
| 208 | 7 | private function buildValueDescriptionsForObjects(ResourceListNode $objectList) { |
|
| 225 | |||
| 226 | 6 | private function formatQueryResult(array $subjectIds) { |
|
| 227 | 6 | $nodes = array(); |
|
| 228 | |||
| 229 | 6 | foreach($subjectIds as $subjectId) { |
|
| 230 | 6 | $nodes[] = new WikibaseResourceNode('', new EntityIdValue($subjectId)); |
|
| 231 | 6 | } |
|
| 232 | |||
| 233 | 6 | return new ResourceListNode($nodes); |
|
| 235 | } |
||
| 236 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: