Conditions | 8 |
Paths | 54 |
Total Lines | 133 |
Code Lines | 88 |
Lines | 10 |
Ratio | 7.52 % |
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 |
||
60 | public function doUpdate() { |
||
61 | $services = MediaWikiServices::getInstance(); |
||
62 | $config = $services->getMainConfig(); |
||
63 | $lbFactory = $services->getDBLoadBalancerFactory(); |
||
64 | $batchSize = $config->get( 'UpdateRowsPerQuery' ); |
||
65 | |||
66 | // Page may already be deleted, so don't just getId() |
||
67 | $id = $this->pageId; |
||
68 | |||
69 | if ( $this->ticket ) { |
||
70 | // Make sure all links update threads see the changes of each other. |
||
71 | // This handles the case when updates have to batched into several COMMITs. |
||
72 | $scopedLock = LinksUpdate::acquirePageLock( $this->getDB(), $id ); |
||
73 | } |
||
74 | |||
75 | $title = $this->page->getTitle(); |
||
76 | $dbw = $this->getDB(); // convenience |
||
77 | |||
78 | // Delete restrictions for it |
||
79 | $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ ); |
||
80 | |||
81 | // Fix category table counts |
||
82 | $cats = $dbw->selectFieldValues( |
||
83 | 'categorylinks', |
||
84 | 'cl_to', |
||
85 | [ 'cl_from' => $id ], |
||
86 | __METHOD__ |
||
87 | ); |
||
88 | $catBatches = array_chunk( $cats, $batchSize ); |
||
89 | foreach ( $catBatches as $catBatch ) { |
||
90 | $this->page->updateCategoryCounts( [], $catBatch, $id ); |
||
91 | View Code Duplication | if ( count( $catBatches ) > 1 ) { |
|
92 | $lbFactory->commitAndWaitForReplication( |
||
93 | __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] |
||
94 | ); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | // Refresh the category table entry if it seems to have no pages. Check |
||
99 | // master for the most up-to-date cat_pages count. |
||
100 | if ( $title->getNamespace() === NS_CATEGORY ) { |
||
101 | $row = $dbw->selectRow( |
||
102 | 'category', |
||
103 | [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ], |
||
104 | [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ], |
||
105 | __METHOD__ |
||
106 | ); |
||
107 | if ( $row ) { |
||
108 | Category::newFromRow( $row, $title )->refreshCounts(); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $this->batchDeleteByPK( |
||
113 | 'pagelinks', |
||
114 | [ 'pl_from' => $id ], |
||
115 | [ 'pl_from', 'pl_namespace', 'pl_title' ], |
||
116 | $batchSize |
||
117 | ); |
||
118 | $this->batchDeleteByPK( |
||
119 | 'imagelinks', |
||
120 | [ 'il_from' => $id ], |
||
121 | [ 'il_from', 'il_to' ], |
||
122 | $batchSize |
||
123 | ); |
||
124 | $this->batchDeleteByPK( |
||
125 | 'categorylinks', |
||
126 | [ 'cl_from' => $id ], |
||
127 | [ 'cl_from', 'cl_to' ], |
||
128 | $batchSize |
||
129 | ); |
||
130 | $this->batchDeleteByPK( |
||
131 | 'templatelinks', |
||
132 | [ 'tl_from' => $id ], |
||
133 | [ 'tl_from', 'tl_namespace', 'tl_title' ], |
||
134 | $batchSize |
||
135 | ); |
||
136 | $this->batchDeleteByPK( |
||
137 | 'externallinks', |
||
138 | [ 'el_from' => $id ], |
||
139 | [ 'el_id' ], |
||
140 | $batchSize |
||
141 | ); |
||
142 | $this->batchDeleteByPK( |
||
143 | 'langlinks', |
||
144 | [ 'll_from' => $id ], |
||
145 | [ 'll_from', 'll_lang' ], |
||
146 | $batchSize |
||
147 | ); |
||
148 | $this->batchDeleteByPK( |
||
149 | 'iwlinks', |
||
150 | [ 'iwl_from' => $id ], |
||
151 | [ 'iwl_from', 'iwl_prefix', 'iwl_title' ], |
||
152 | $batchSize |
||
153 | ); |
||
154 | |||
155 | // Delete any redirect entry or page props entries |
||
156 | $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ ); |
||
157 | $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ ); |
||
158 | |||
159 | // Find recentchanges entries to clean up... |
||
160 | $rcIdsForTitle = $dbw->selectFieldValues( |
||
161 | 'recentchanges', |
||
162 | 'rc_id', |
||
163 | [ |
||
164 | 'rc_type != ' . RC_LOG, |
||
165 | 'rc_namespace' => $title->getNamespace(), |
||
166 | 'rc_title' => $title->getDBkey(), |
||
167 | 'rc_timestamp < ' . |
||
168 | $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ) |
||
169 | ], |
||
170 | __METHOD__ |
||
171 | ); |
||
172 | $rcIdsForPage = $dbw->selectFieldValues( |
||
173 | 'recentchanges', |
||
174 | 'rc_id', |
||
175 | [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ], |
||
176 | __METHOD__ |
||
177 | ); |
||
178 | |||
179 | // T98706: delete by PK to avoid lock contention with RC delete log insertions |
||
180 | $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize ); |
||
181 | foreach ( $rcIdBatches as $rcIdBatch ) { |
||
182 | $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ ); |
||
183 | View Code Duplication | if ( count( $rcIdBatches ) > 1 ) { |
|
184 | $lbFactory->commitAndWaitForReplication( |
||
185 | __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] |
||
186 | ); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | // Commit and release the lock (if set) |
||
191 | ScopedCallback::consume( $scopedLock ); |
||
192 | } |
||
193 | |||
238 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: