Conditions | 13 |
Paths | 608 |
Total Lines | 74 |
Code Lines | 49 |
Lines | 7 |
Ratio | 9.46 % |
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 |
||
55 | private function run( $resultPageSet = null ) { |
||
56 | $params = $this->extractRequestParams(); |
||
57 | |||
58 | $prop = array_flip( $params['prop'] ); |
||
59 | $fld_ids = isset( $prop['ids'] ); |
||
60 | $fld_title = isset( $prop['title'] ); |
||
61 | $fld_value = isset( $prop['value'] ); |
||
62 | |||
63 | View Code Duplication | if ( $resultPageSet === null ) { |
|
64 | $this->addFields( [ 'page_id' ] ); |
||
65 | $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title ); |
||
66 | $this->addFieldsIf( 'pp_value', $fld_value ); |
||
67 | } else { |
||
68 | $this->addFields( $resultPageSet->getPageTableFields() ); |
||
69 | } |
||
70 | $this->addTables( [ 'page_props', 'page' ] ); |
||
71 | $this->addWhere( 'pp_page=page_id' ); |
||
72 | $this->addWhereFld( 'pp_propname', $params['propname'] ); |
||
73 | |||
74 | $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older'; |
||
75 | |||
76 | if ( $params['continue'] ) { |
||
77 | $cont = explode( '|', $params['continue'] ); |
||
78 | $this->dieContinueUsageIf( count( $cont ) != 1 ); |
||
79 | |||
80 | // Add a WHERE clause |
||
81 | $from = (int)$cont[0]; |
||
82 | $this->addWhereRange( 'pp_page', $dir, $from, null ); |
||
83 | } |
||
84 | |||
85 | $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' ); |
||
86 | $this->addOption( 'ORDER BY', 'pp_page' . $sort ); |
||
87 | |||
88 | $limit = $params['limit']; |
||
89 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
90 | |||
91 | $result = $this->getResult(); |
||
92 | $count = 0; |
||
93 | foreach ( $this->select( __METHOD__ ) as $row ) { |
||
94 | if ( ++$count > $limit ) { |
||
95 | // We've reached the one extra which shows that there are |
||
96 | // additional pages to be had. Stop here... |
||
97 | $this->setContinueEnumParameter( 'continue', $row->page_id ); |
||
98 | break; |
||
99 | } |
||
100 | |||
101 | if ( $resultPageSet === null ) { |
||
102 | $vals = [ |
||
103 | ApiResult::META_TYPE => 'assoc', |
||
104 | ]; |
||
105 | if ( $fld_ids ) { |
||
106 | $vals['pageid'] = (int)$row->page_id; |
||
107 | } |
||
108 | if ( $fld_title ) { |
||
109 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
||
110 | ApiQueryBase::addTitleInfo( $vals, $title ); |
||
111 | } |
||
112 | if ( $fld_value ) { |
||
113 | $vals['value'] = $row->pp_value; |
||
114 | } |
||
115 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
||
116 | if ( !$fit ) { |
||
117 | $this->setContinueEnumParameter( 'continue', $row->page_id ); |
||
118 | break; |
||
119 | } |
||
120 | } else { |
||
121 | $resultPageSet->processDbRow( $row ); |
||
|
|||
122 | } |
||
123 | } |
||
124 | |||
125 | if ( $resultPageSet === null ) { |
||
126 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' ); |
||
127 | } |
||
128 | } |
||
129 | |||
179 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: