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 | } |
||
60 | |||
61 | /** |
||
62 | * @see AbstractNode::isSimplifierFor |
||
63 | */ |
||
64 | 5 | public function isSimplifierFor(AbstractNode $node) { |
|
67 | |||
68 | 5 | private function isTripleWithMissingSubject(AbstractNode $node) { |
|
69 | 5 | return $node instanceof TripleNode && |
|
70 | 1 | $node->getSubject() instanceof MissingNode; |
|
71 | 5 | } |
|
72 | |||
73 | 4 | private function isTripleWithMissingSubjectOperator(AbstractNode $node) { |
|
74 | 3 | if(!($node instanceof IntersectionNode || $node instanceof UnionNode)) { |
|
75 | 4 | return false; |
|
76 | } |
||
77 | |||
78 | foreach($node->getOperands() as $operand) { |
||
79 | if(!$this->isSimplifierFor($operand)) { |
||
80 | return false; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return true; |
||
85 | 3 | } |
|
86 | |||
87 | /** |
||
88 | * @see NodeSimplifier::doSimplification |
||
89 | */ |
||
90 | 14 | public function simplify(AbstractNode $node) { |
|
91 | try { |
||
92 | 8 | $query = $this->buildQueryForNode($node); |
|
93 | } catch(EmptyQueryException $e) { |
||
94 | return new ResourceListNode(); |
||
95 | 10 | } |
|
96 | |||
97 | $entityIds = $this->entityStore->getItemIdForQueryLookup()->getItemIdsForQuery($query, new QueryOptions(self::QUERY_LIMIT, 0)); |
||
98 | |||
99 | return $this->formatQueryResult($entityIds); |
||
100 | 14 | } |
|
101 | |||
102 | 12 | 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) { |
|
134 | |||
135 | 3 | private function buildQueryForIntersection(IntersectionNode $intersectionNode) { |
|
149 | |||
150 | 11 | private function buildQueryForTriple(TripleNode $triple) { |
|
186 | |||
187 | 8 | private function bagsPropertiesPerType($propertyNodes) { |
|
200 | |||
201 | 4 | private function buildQueryForProperty(WikibaseResourceNode $predicate, ResourceListNode $objectList) { |
|
207 | |||
208 | 4 | private function buildValueDescriptionsForObjects(ResourceListNode $objectList) { |
|
225 | |||
226 | 6 | private function formatQueryResult(array $subjectIds) { |
|
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: