| Conditions | 2 |
| Paths | 1 |
| Total Lines | 69 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 44 | public function getReferenceValue($identifier) |
||
| 45 | { |
||
| 46 | $identifier = trim($this->getReferenceIdentifier($identifier)); |
||
| 47 | |||
| 48 | /* |
||
| 49 | // original idea: regexp-based parsing |
||
| 50 | // supported: |
||
| 51 | // someOperator |
||
| 52 | // someOperator(a, b) |
||
| 53 | // someOperator|filter |
||
| 54 | // someOperator(a, b)|filter |
||
| 55 | // someOperator(a, b)|filter(c, d) |
||
| 56 | /// @todo this way of matching args does not allow for arguments which contain a comma, right parenthesis or pipe chars... |
||
| 57 | if (!preg_match('/^([^(|]+)(?:\(([^)]+)\))?(?:\|(unique|optional|valid)(?:\(([^)]+)\))?)?$/', $identifier, $matches)) { |
||
| 58 | throw new \Exception("Could not parse as faker identifier ths string '$identifier'."); |
||
| 59 | } |
||
| 60 | |||
| 61 | $formatter = trim($matches[1]); |
||
| 62 | $args = isset($matches[2]) ? trim($matches[2]) : ''; |
||
| 63 | $filter = isset($matches[3]) ? trim($matches[3]) : ''; |
||
| 64 | $filterArgs = isset($matches[4]) ? trim($matches[4]) : ''; |
||
| 65 | if ($args != '') |
||
| 66 | { |
||
| 67 | /// @todo convert true, false to bools, strip strings of quotes |
||
| 68 | $arguments = array_map('trim', explode(',', $args)); |
||
| 69 | } else { |
||
| 70 | $arguments = array(); |
||
| 71 | } |
||
| 72 | if ($filterArgs != '') |
||
| 73 | { |
||
| 74 | /// @todo convert true, false to bools, strip strings of quotes |
||
| 75 | $filterArguments = array_map('trim', explode(',', $filterArgs)); |
||
| 76 | } |
||
| 77 | else { |
||
| 78 | $filterArguments = array(); |
||
| 79 | } |
||
| 80 | |||
| 81 | $faker = $this->faker; |
||
| 82 | |||
| 83 | if ($filter != '') { |
||
| 84 | $faker = $this->faker->format($filter, $filterArguments); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $faker->format($formatter, $arguments); |
||
| 88 | */ |
||
| 89 | |||
| 90 | // alternative idea: use symfony/expression-language |
||
| 91 | $expressionLanguage = new ExpressionLanguage(); |
||
| 92 | |||
| 93 | $resolver = $this->referenceResolver; |
||
| 94 | |||
| 95 | $expressionLanguage->register( |
||
| 96 | 'resolve', |
||
| 97 | function ($str) { |
||
|
|
|||
| 98 | /// @todo we could implement this via eg a static class var which holds a pointer to $this->referenceResolver |
||
| 99 | //return sprintf('(is_string(%1$s) ? FakerResolver::resolveExpressionLanguageReference(%1$s) : %1$s)', $str); |
||
| 100 | return "throw new \Exception('The \'resolve\' expression language operator can not be compiled, only evaluated'"; |
||
| 101 | }, |
||
| 102 | function ($arguments, $str) use ($resolver) { |
||
| 103 | if (!is_string($str)) { |
||
| 104 | return $str; |
||
| 105 | } |
||
| 106 | |||
| 107 | return $resolver->resolveReference($str); |
||
| 108 | } |
||
| 109 | ); |
||
| 110 | |||
| 111 | return $expressionLanguage->evaluate('faker.' . $identifier, array( |
||
| 112 | 'faker' => $this->faker, |
||
| 113 | )); |
||
| 136 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.