| Conditions | 9 |
| Paths | 9 |
| Total Lines | 37 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 90 |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 48 | public static function getBuild(Build $build) |
||
| 49 | { |
||
| 50 | $project = $build->getProject(); |
||
| 51 | |||
| 52 | if (!empty($project)) { |
||
| 53 | switch ($project->getType()) { |
||
| 54 | case 'remote': |
||
| 55 | $type = 'RemoteGitBuild'; |
||
| 56 | break; |
||
| 57 | case 'local': |
||
| 58 | $type = 'LocalBuild'; |
||
| 59 | break; |
||
| 60 | case 'github': |
||
| 61 | $type = 'GithubBuild'; |
||
| 62 | break; |
||
| 63 | case 'bitbucket': |
||
| 64 | $type = 'BitbucketBuild'; |
||
| 65 | break; |
||
| 66 | case 'gitlab': |
||
| 67 | $type = 'GitlabBuild'; |
||
| 68 | break; |
||
| 69 | case 'hg': |
||
| 70 | $type = 'MercurialBuild'; |
||
| 71 | break; |
||
| 72 | case 'svn': |
||
| 73 | $type = 'SubversionBuild'; |
||
| 74 | break; |
||
| 75 | default: |
||
| 76 | return $build; |
||
| 77 | } |
||
| 78 | |||
| 79 | $class = '\\PHPCI\\Model\\Build\\'.$type; |
||
| 80 | $build = new $class($build->getDataArray()); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $build; |
||
| 84 | } |
||
| 85 | } |
||
| 86 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: