| Conditions | 9 |
| Paths | 66 |
| Total Lines | 58 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 2 | Features | 3 |
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 |
||
| 42 | public function create( \Aimeos\MW\Config\Iface $config, |
||
| 43 | \TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder, array $templatePaths, |
||
| 44 | \TYPO3\Flow\Mvc\RequestInterface $request = null, $langid = null ) |
||
| 45 | { |
||
| 46 | $params = $fixed = array(); |
||
| 47 | |||
| 48 | if( $request !== null && $langid !== null ) |
||
| 49 | { |
||
| 50 | $params = $request->getArguments(); |
||
| 51 | $fixed = $this->getFixedParams( $request ); |
||
| 52 | |||
| 53 | $i18n = $this->i18n->get( array( $langid ) ); |
||
| 54 | $translation = $i18n[$langid]; |
||
| 55 | } |
||
| 56 | else |
||
| 57 | { |
||
| 58 | $translation = new \Aimeos\MW\Translation\None( 'en' ); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | $view = new \Aimeos\MW\View\Standard( $templatePaths ); |
||
| 63 | |||
| 64 | $helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation ); |
||
| 65 | $view->addHelper( 'translate', $helper ); |
||
| 66 | |||
| 67 | $helper = new \Aimeos\MW\View\Helper\Url\Flow( $view, $uriBuilder, $fixed ); |
||
| 68 | $view->addHelper( 'url', $helper ); |
||
| 69 | |||
| 70 | $helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params ); |
||
| 71 | $view->addHelper( 'param', $helper ); |
||
| 72 | |||
| 73 | $helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config ); |
||
| 74 | $view->addHelper( 'config', $helper ); |
||
| 75 | |||
| 76 | $sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' ); |
||
| 77 | $sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' ); |
||
| 78 | $helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000 ); |
||
| 79 | $view->addHelper( 'number', $helper ); |
||
| 80 | |||
| 81 | if( $request !== null ) |
||
| 82 | { |
||
| 83 | $req = $request->getHttpRequest(); |
||
|
|
|||
| 84 | |||
| 85 | $files = ( is_array( $_FILES ) ? $_FILES : array() ); |
||
| 86 | $query = ( is_array( $_GET ) ? $_GET : array() ); |
||
| 87 | $post = ( is_array( $_POST ) ? $_POST : array() ); |
||
| 88 | $cookie = ( is_array( $_COOKIE ) ? $_COOKIE : array() ); |
||
| 89 | $server = ( is_array( $_SERVER ) ? $_SERVER : array() ); |
||
| 90 | |||
| 91 | $helper = new \Aimeos\MW\View\Helper\Request\Flow( $view, $req, $files, $query, $post, $cookie, $server ); |
||
| 92 | $view->addHelper( 'request', $helper ); |
||
| 93 | } |
||
| 94 | |||
| 95 | $helper = new \Aimeos\MW\View\Helper\Response\Flow( $view ); |
||
| 96 | $view->addHelper( 'response', $helper ); |
||
| 97 | |||
| 98 | return $view; |
||
| 99 | } |
||
| 100 | |||
| 128 | } |
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 implementation 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 interface: