| Conditions | 31 |
| Paths | 5044 |
| Total Lines | 122 |
| 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 |
||
| 21 | public function find( $new_declarations, $prev_declarations, $new_root = null, $find_deprecated = false ) { |
||
| 22 | if ( $new_root ) { |
||
| 23 | $new_root = $this->slashit( $new_root ); |
||
| 24 | } else { |
||
| 25 | echo "Warning: calling find() without \$new_root means we can't detect if files are stubbed in the new release\n"; |
||
| 26 | } |
||
| 27 | $total = 0; |
||
| 28 | $missing_total = 0; |
||
| 29 | $moved_total = 0; |
||
| 30 | $moved_with_empty_file_total = 0; |
||
| 31 | $deprecated_total = 0; |
||
| 32 | // for each declaration, see if it exists in the current analyzer's declarations |
||
| 33 | // if not, add it to the list of differences - either as missing or different |
||
| 34 | foreach ( $prev_declarations->get() as $prev_declaration ) { |
||
| 35 | $matched = false; |
||
| 36 | $moved = false; |
||
| 37 | $moved_with_empty_file = false; |
||
| 38 | $deprecated = false; |
||
| 39 | foreach ( $new_declarations->get() as $new_declaration ) { |
||
| 40 | |||
| 41 | if ( $prev_declaration->match( $new_declaration ) ) { |
||
| 42 | if ( $find_deprecated && isset( $new_declaration->deprecated ) && $new_declaration->deprecated ) { |
||
| 43 | $deprecated = true; |
||
| 44 | } |
||
| 45 | |||
| 46 | // echo "Comparing " . $prev_declaration->path . " to " . $new_declaration->path . "\n"; |
||
| 47 | if ( $prev_declaration->path !== $new_declaration->path ) { |
||
| 48 | |||
| 49 | // if a file exists at the old location, and the new method is (we assume) autoloaded, |
||
| 50 | // do not warn. |
||
| 51 | // TODO: since functions are not autoloaded, we should probably still warn for them? |
||
| 52 | if ( $new_root && file_exists( $new_root . $prev_declaration->path ) ) { |
||
| 53 | $moved_with_empty_file = true; |
||
| 54 | $moved_with_empty_file_total += 1; |
||
| 55 | } else { |
||
| 56 | $moved = true; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | $matched = true; |
||
| 60 | break; |
||
| 61 | } elseif ( $prev_declaration->partial_match( $new_declaration ) ) { |
||
| 62 | // TODO this is to catch things like function args changed, method the same |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // do not add warnings for $moved_with_empty_file |
||
| 67 | if ( $matched && $moved_with_empty_file ) { |
||
| 68 | // echo "Declaration " . $prev_declaration->display_name() . " moved from " . $prev_declaration->path . " to " . $new_declaration->path . " with matching empty file at original location\n"; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Add differences for any detected deprecations. |
||
| 72 | if ( $deprecated ) { |
||
| 73 | switch ( $new_declaration->type() ) { |
||
| 74 | case 'method': |
||
| 75 | $this->add( new Differences\Class_Method_Deprecated( $prev_declaration, $new_declaration ) ); |
||
| 76 | break; |
||
| 77 | case 'function': |
||
| 78 | $this->add( new Differences\Function_Deprecated( $prev_declaration, $new_declaration ) ); |
||
| 79 | break; |
||
| 80 | default: |
||
| 81 | echo 'Unknown deprecated type ' . $new_declaration->type() . "\n"; |
||
| 82 | } |
||
| 83 | $deprecated_total++; |
||
| 84 | |||
| 85 | } elseif ( $matched && $moved ) { |
||
| 86 | switch ( $prev_declaration->type() ) { |
||
| 87 | case 'class': |
||
| 88 | $this->add( new Differences\Class_Moved( $prev_declaration, $new_declaration ) ); |
||
| 89 | break; |
||
| 90 | case 'class_const': |
||
| 91 | $this->add( new Differences\Class_Const_Moved( $prev_declaration, $new_declaration ) ); |
||
| 92 | break; |
||
| 93 | case 'method': |
||
| 94 | $this->add( new Differences\Class_Method_Moved( $prev_declaration, $new_declaration ) ); |
||
| 95 | break; |
||
| 96 | case 'property': |
||
| 97 | $this->add( new Differences\Class_Property_Moved( $prev_declaration, $new_declaration ) ); |
||
| 98 | break; |
||
| 99 | case 'function': |
||
| 100 | $this->add( new Differences\Function_Moved( $prev_declaration, $new_declaration ) ); |
||
| 101 | break; |
||
| 102 | default: |
||
| 103 | echo 'Unknown moved type ' . $prev_declaration->type() . "\n"; |
||
| 104 | } |
||
| 105 | $moved_total += 1; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( ! $matched ) { |
||
| 109 | switch ( $prev_declaration->type() ) { |
||
| 110 | case 'class': |
||
| 111 | $this->add( new Differences\Class_Missing( $prev_declaration ) ); |
||
| 112 | break; |
||
| 113 | case 'class_const': |
||
| 114 | $this->add( new Differences\Class_Const_Missing( $prev_declaration ) ); |
||
| 115 | break; |
||
| 116 | case 'method': |
||
| 117 | $this->add( new Differences\Class_Method_Missing( $prev_declaration ) ); |
||
| 118 | break; |
||
| 119 | case 'property': |
||
| 120 | $this->add( new Differences\Class_Property_Missing( $prev_declaration ) ); |
||
| 121 | break; |
||
| 122 | case 'function': |
||
| 123 | $this->add( new Differences\Function_Missing( $prev_declaration ) ); |
||
| 124 | break; |
||
| 125 | default: |
||
| 126 | echo 'Unknown unmatched type ' . $prev_declaration->type() . "\n"; |
||
| 127 | } |
||
| 128 | $missing_total += 1; |
||
| 129 | } |
||
| 130 | |||
| 131 | $total += 1; |
||
| 132 | } |
||
| 133 | |||
| 134 | echo "Total Declarations: $total\n"; |
||
| 135 | echo 'Total Differences: ' . count( $this->get() ) . "\n"; |
||
| 136 | echo 'Moved: ' . $moved_total . "\n"; |
||
| 137 | echo 'Moved with stubbed file: ' . $moved_with_empty_file_total . "\n"; |
||
| 138 | echo 'Missing: ' . $missing_total . "\n"; |
||
| 139 | if ( $find_deprecated ) { |
||
| 140 | echo 'Deprecated: ' . $deprecated_total . "\n"; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.