Conditions | 9 |
Paths | 26 |
Total Lines | 56 |
Code Lines | 35 |
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 |
||
90 | public function descend( Page $rootCat, $currentPath = null ) { |
||
91 | // Make sure we know the namespace IDs. |
||
92 | $this->retrieveNamespaces(); |
||
93 | |||
94 | $rootCatName = $rootCat->getPageIdentifier()->getTitle()->getText(); |
||
95 | if ( is_null( $currentPath ) ) { |
||
96 | $this->alreadyVisited = []; |
||
97 | $currentPath = new Pages(); |
||
98 | } |
||
99 | $this->alreadyVisited[] = $rootCatName; |
||
100 | $currentPath->addPage( $rootCat ); |
||
|
|||
101 | |||
102 | // Start a list of child pages. |
||
103 | $descendants = new Pages(); |
||
104 | do { |
||
105 | $pageListGetter = new PageListGetter( $this->api ); |
||
106 | $members = $pageListGetter->getPageListFromCategoryName( $rootCatName ); |
||
107 | foreach ( $members->toArray() as $member ) { |
||
108 | /** @var Title */ |
||
109 | $memberTitle = $member->getPageIdentifier()->getTitle(); |
||
110 | |||
111 | // See if this page is a Category page. |
||
112 | $isCat = false; |
||
113 | if ( isset( $this->namespaces[ $memberTitle->getNs() ] ) ) { |
||
114 | $ns = $this->namespaces[ $memberTitle->getNs() ]; |
||
115 | $isCat = ( isset( $ns['canonical'] ) && $ns['canonical'] === 'Category' ); |
||
116 | } |
||
117 | // If it's a category, descend into it. |
||
118 | if ( $isCat ) { |
||
119 | // If this member has already been visited on this branch of the traversal, |
||
120 | // throw an Exception with information about which categories form the loop. |
||
121 | if ( $currentPath->hasPage( $member ) ) { |
||
122 | $currentPath->addPage( $member ); |
||
123 | $loop = new CategoryLoopException(); |
||
124 | $loop->setCategoryPath( $currentPath ); |
||
125 | throw $loop; |
||
126 | } |
||
127 | // Don't go any further if we've already visited this member |
||
128 | // (does not indicate a loop, however; we've already caught that above). |
||
129 | if ( in_array( $memberTitle->getText(), $this->alreadyVisited ) ) { |
||
130 | continue; |
||
131 | } |
||
132 | // Call any registered callbacked, and carry on to the next branch. |
||
133 | $this->call( self::CALLBACK_CATEGORY, [ $member, $rootCat ] ); |
||
134 | $newDescendants = $this->descend( $member, $currentPath ); |
||
135 | $descendants->addPages( $newDescendants ); |
||
136 | $currentPath = new Pages(); // Re-set the path. |
||
137 | } else { |
||
138 | // If it's a page, add it to the list and carry on. |
||
139 | $descendants->addPage( $member ); |
||
140 | $this->call( self::CALLBACK_PAGE, [ $member, $rootCat ] ); |
||
141 | } |
||
142 | } |
||
143 | } while ( isset( $result['continue'] ) ); |
||
144 | return $descendants; |
||
145 | } |
||
146 | |||
164 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: