Conditions | 1 |
Paths | 1 |
Total Lines | 76 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
50 | public function testHasOneHook() |
||
51 | { |
||
52 | $hasOne = new SearchUpdaterTest_HasOne(); |
||
53 | $hasOne->write(); |
||
54 | |||
55 | $alternateHasOne = new SearchUpdaterTest_HasOne(); |
||
56 | $alternateHasOne->write(); |
||
57 | |||
58 | $container1 = new SearchUpdaterTest_Container(); |
||
59 | $container1->HasOneObjectID = $hasOne->ID; |
||
|
|||
60 | $container1->write(); |
||
61 | |||
62 | $container2 = new SearchUpdaterTest_Container(); |
||
63 | $container2->HasOneObjectID = $hasOne->ID; |
||
64 | $container2->write(); |
||
65 | |||
66 | $container3 = new SearchUpdaterTest_Container(); |
||
67 | $container3->HasOneObjectID = $alternateHasOne->ID; |
||
68 | $container3->write(); |
||
69 | |||
70 | // Check the default "writing a document updates the document" |
||
71 | SearchUpdater::flush_dirty_indexes(); |
||
72 | |||
73 | $added = self::$index->getAdded(['ID']); |
||
74 | // Some databases don't output $added in a consistent order; that's okay |
||
75 | usort($added, function ($a, $b) { |
||
76 | return $a['ID']-$b['ID']; |
||
77 | }); |
||
78 | |||
79 | $this->assertEquals([ |
||
80 | ['ID' => $container1->ID], |
||
81 | ['ID' => $container2->ID], |
||
82 | ['ID' => $container3->ID], |
||
83 | ], $added); |
||
84 | |||
85 | // Check writing a has_one tracks back to the origin documents |
||
86 | |||
87 | self::$index->reset(); |
||
88 | |||
89 | $hasOne->Field1 = "Updated"; |
||
90 | $hasOne->write(); |
||
91 | |||
92 | SearchUpdater::flush_dirty_indexes(); |
||
93 | $added = self::$index->getAdded(['ID']); |
||
94 | |||
95 | // Some databases don't output $added in a consistent order; that's okay |
||
96 | usort($added, function ($a, $b) { |
||
97 | return $a['ID']-$b['ID']; |
||
98 | }); |
||
99 | |||
100 | $this->assertEquals([ |
||
101 | ['ID' => $container1->ID], |
||
102 | ['ID' => $container2->ID], |
||
103 | ], $added); |
||
104 | |||
105 | // Check updating an unrelated field doesn't track back |
||
106 | |||
107 | self::$index->reset(); |
||
108 | |||
109 | $hasOne->Field2 = "Updated"; |
||
110 | $hasOne->write(); |
||
111 | |||
112 | SearchUpdater::flush_dirty_indexes(); |
||
113 | $this->assertEquals([], self::$index->getAdded(['ID'])); |
||
114 | |||
115 | // Check writing a has_one tracks back to the origin documents |
||
116 | |||
117 | self::$index->reset(); |
||
118 | |||
119 | $alternateHasOne->Field1= "Updated"; |
||
120 | $alternateHasOne->write(); |
||
121 | |||
122 | SearchUpdater::flush_dirty_indexes(); |
||
123 | $this->assertEquals([ |
||
124 | ['ID' => $container3->ID], |
||
125 | ], self::$index->getAdded(['ID'])); |
||
126 | } |
||
168 |