| Conditions | 14 |
| Paths | 16 |
| Total Lines | 77 |
| 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 |
||
| 108 | protected function handleParameters( array $params, $outputmode ) {
|
||
| 109 | |||
| 110 | parent::handleParameters( $params, $outputmode ); |
||
| 111 | |||
| 112 | //Set header params |
||
| 113 | $this->mParams['title'] = trim( $params['diagramtitle'] ); |
||
| 114 | $this->mParams['axisformat'] = trim( $params['axisformat'] ); |
||
| 115 | $this->mParams['sortkey'] = trim( $params['sortkey'] ); |
||
| 116 | $this->mParams['statusmapping'] = trim( $params['statusmapping'] ); |
||
| 117 | $this->mParams['prioritymapping'] = trim( $params['prioritymapping'] ); |
||
| 118 | $this->mParams['theme'] = trim( $params['theme'] ); |
||
| 119 | |||
| 120 | //Validate Theme |
||
| 121 | if ( !in_array( $this->params['theme'], [ "default", "neutral", "dark", "forest" ] ) ) {
|
||
| 122 | array_push( $this->mErrors, wfMessage( 'srf-error-gantt-theme' )->text() ); |
||
| 123 | } |
||
| 124 | |||
| 125 | //Validate sortkey |
||
| 126 | if ( !in_array( strtolower( $this->params['sortkey'] ), [ "title", "startdate", "enddate" ] ) ) {
|
||
| 127 | array_push( $this->mErrors, wfMessage( 'srf-error-gantt-sortkey' )->text() ); |
||
| 128 | } |
||
| 129 | |||
| 130 | //Validate mapping |
||
| 131 | if ( !empty( trim( $params['statusmapping'] ) ) ) {
|
||
| 132 | |||
| 133 | $paramMapping = explode( ';', trim( $params['statusmapping'] ) ); |
||
| 134 | |||
| 135 | foreach ( $paramMapping as $pm ) {
|
||
| 136 | |||
| 137 | // if no "=>" pattern was found |
||
| 138 | if ( !strpos( $pm, '=>' ) ) {
|
||
| 139 | array_push( $this->mErrors, wfMessage( 'srf-error-gantt-mapping-assignment', 'statusmapping' )->text() ); |
||
| 140 | } else {
|
||
| 141 | $pmKeyVal = explode( '=>', $pm ); |
||
| 142 | // if no key value pair |
||
| 143 | if ( count( $pmKeyVal ) % 2 != 0 ) {
|
||
| 144 | array_push( $this->mErrors, |
||
| 145 | wfMessage( 'srf-error-gantt-mapping-assignment', 'statusmapping' )->text() ); |
||
| 146 | } else {
|
||
| 147 | $mapping[trim( $pmKeyVal[0] )] = trim( $pmKeyVal[1] ); |
||
|
|
|||
| 148 | // check if the common status keys are used |
||
| 149 | if ( trim( $pmKeyVal[1] ) != "active" && trim( $pmKeyVal[1] ) != "done" ) {
|
||
| 150 | array_push( $this->mErrors, wfMessage( 'srf-error-gantt-mapping-keywords' )->text() ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | if ( !empty( trim( $params['prioritymapping'] ) ) ) {
|
||
| 157 | |||
| 158 | $paramMapping = explode( ';', trim( $params['prioritymapping'] ) ); |
||
| 159 | |||
| 160 | foreach ( $paramMapping as $pm ) {
|
||
| 161 | |||
| 162 | // if no "=>" pattern was found |
||
| 163 | if ( !strpos( $pm, '=>' ) ) {
|
||
| 164 | array_push( $this->mErrors, |
||
| 165 | wfMessage( 'srf-error-gantt-mapping-assignment', 'prioritymapping' )->text() ); |
||
| 166 | } else {
|
||
| 167 | $pmKeyVal = explode( '=>', $pm ); |
||
| 168 | // if no key value pair |
||
| 169 | if ( count( $pmKeyVal ) % 2 != 0 ) {
|
||
| 170 | array_push( $this->mErrors, |
||
| 171 | wfMessage( 'srf-error-gantt-mapping-assignment', 'statusmapping' )->text() ); |
||
| 172 | } else {
|
||
| 173 | $mapping[trim( $pmKeyVal[0] )] = trim( $pmKeyVal[1] ); |
||
| 174 | // check if the common status keys are used |
||
| 175 | if ( trim( $pmKeyVal[1] ) != "crit" ) {
|
||
| 176 | array_push( $this->mErrors, wfMessage( 'srf-error-gantt-mapping-keywords' )->text() ); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->mGantt = new Gantt( $this->mParams ); |
||
| 184 | } |
||
| 185 | |||
| 307 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.