| Conditions | 1 |
| Paths | 1 |
| Total Lines | 128 |
| Code Lines | 116 |
| 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 |
||
| 33 | private function createAccountsNode() |
||
| 34 | { |
||
| 35 | $treeBuilder = new TreeBuilder(); |
||
| 36 | $node = $treeBuilder->root('accounts'); |
||
| 37 | |||
| 38 | $node |
||
|
|
|||
| 39 | ->useAttributeAsKey('name') |
||
| 40 | ->prototype('array') |
||
| 41 | ->children() |
||
| 42 | ->scalarNode('public_key')->defaultValue(null)->end() |
||
| 43 | |||
| 44 | ->arrayNode('parameters') |
||
| 45 | ->isRequired() |
||
| 46 | ->children() |
||
| 47 | ->scalarNode('production')->defaultValue(false)->end() |
||
| 48 | ->arrayNode('currencies') |
||
| 49 | ->defaultValue(array( |
||
| 50 | '036', // AUD |
||
| 51 | '124', // CAD |
||
| 52 | '756', // CHF |
||
| 53 | '826', // GBP |
||
| 54 | '840', // USD |
||
| 55 | '978', // EUR |
||
| 56 | )) |
||
| 57 | ->prototype('scalar')->end() |
||
| 58 | ->end() |
||
| 59 | ->scalarNode('site')->isRequired()->end() |
||
| 60 | ->scalarNode('rank')->defaultValue(null)->end() |
||
| 61 | ->scalarNode('rang')->defaultValue(null)->end() |
||
| 62 | ->scalarNode('login')->defaultValue(null)->end() |
||
| 63 | ->scalarNode('cle')->defaultValue(null)->end() |
||
| 64 | ->arrayNode('hmac') |
||
| 65 | ->isRequired() |
||
| 66 | ->children() |
||
| 67 | ->scalarNode('algorithm')->defaultValue('sha512')->end() |
||
| 68 | ->scalarNode('key')->isRequired()->end() |
||
| 69 | ->scalarNode('signature_name')->defaultValue('Sign')->end() |
||
| 70 | ->end() |
||
| 71 | ->end() |
||
| 72 | ->end() |
||
| 73 | ->end() |
||
| 74 | |||
| 75 | ->arrayNode('servers') |
||
| 76 | ->addDefaultsIfNotSet() |
||
| 77 | ->children() |
||
| 78 | |||
| 79 | ->arrayNode('system') |
||
| 80 | ->addDefaultsIfNotSet() |
||
| 81 | ->children() |
||
| 82 | ->arrayNode('primary') |
||
| 83 | ->addDefaultsIfNotSet() |
||
| 84 | ->children() |
||
| 85 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 86 | ->scalarNode('host')->defaultValue('tpeweb.paybox.com')->end() |
||
| 87 | ->scalarNode('system_path')->defaultValue('/cgi/MYchoix_pagepaiement.cgi')->end() |
||
| 88 | ->scalarNode('cancellation_path')->defaultValue('/cgi-bin/ResAbon.cgi')->end() |
||
| 89 | ->scalarNode('test_path')->defaultValue('/load.html')->end() |
||
| 90 | ->end() |
||
| 91 | ->end() |
||
| 92 | ->arrayNode('secondary') |
||
| 93 | ->addDefaultsIfNotSet() |
||
| 94 | ->children() |
||
| 95 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 96 | ->scalarNode('host')->defaultValue('tpeweb1.paybox.com')->end() |
||
| 97 | ->scalarNode('system_path')->defaultValue('/cgi/MYchoix_pagepaiement.cgi')->end() |
||
| 98 | ->scalarNode('cancellation_path')->defaultValue('/cgi-bin/ResAbon.cgi')->end() |
||
| 99 | ->scalarNode('test_path')->defaultValue('/load.html')->end() |
||
| 100 | ->end() |
||
| 101 | ->end() |
||
| 102 | ->arrayNode('preprod') |
||
| 103 | ->addDefaultsIfNotSet() |
||
| 104 | ->children() |
||
| 105 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 106 | ->scalarNode('host')->defaultValue('preprod-tpeweb.paybox.com')->end() |
||
| 107 | ->scalarNode('system_path')->defaultValue('/cgi/MYchoix_pagepaiement.cgi')->end() |
||
| 108 | ->scalarNode('cancellation_path')->defaultValue('/cgi-bin/ResAbon.cgi')->end() |
||
| 109 | ->scalarNode('test_path')->defaultValue('/load.html')->end() |
||
| 110 | ->end() |
||
| 111 | ->end() |
||
| 112 | ->end() |
||
| 113 | ->end() |
||
| 114 | |||
| 115 | ->arrayNode('direct_plus') |
||
| 116 | ->addDefaultsIfNotSet() |
||
| 117 | ->children() |
||
| 118 | ->arrayNode('primary') |
||
| 119 | ->addDefaultsIfNotSet() |
||
| 120 | ->children() |
||
| 121 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 122 | ->scalarNode('host')->defaultValue('ppps.paybox.com')->end() |
||
| 123 | ->scalarNode('api_path')->defaultValue('/PPPS.php')->end() |
||
| 124 | ->end() |
||
| 125 | ->end() |
||
| 126 | ->arrayNode('secondary') |
||
| 127 | ->addDefaultsIfNotSet() |
||
| 128 | ->children() |
||
| 129 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 130 | ->scalarNode('host')->defaultValue('ppps1.paybox.com')->end() |
||
| 131 | ->scalarNode('api_path')->defaultValue('/PPPS.php')->end() |
||
| 132 | ->end() |
||
| 133 | ->end() |
||
| 134 | ->arrayNode('preprod') |
||
| 135 | ->addDefaultsIfNotSet() |
||
| 136 | ->children() |
||
| 137 | ->scalarNode('protocol')->defaultValue('https')->end() |
||
| 138 | ->scalarNode('host')->defaultValue('preprod-ppps.paybox.com')->end() |
||
| 139 | ->scalarNode('api_path')->defaultValue('/PPPS.php')->end() |
||
| 140 | ->end() |
||
| 141 | ->end() |
||
| 142 | ->end() |
||
| 143 | ->end() |
||
| 144 | |||
| 145 | ->end() |
||
| 146 | ->end() |
||
| 147 | |||
| 148 | ->scalarNode('transport') |
||
| 149 | ->defaultValue('Lexik\Bundle\PayboxBundle\Transport\CurlTransport') |
||
| 150 | ->validate() |
||
| 151 | ->ifTrue(function($v) { return !class_exists($v); }) |
||
| 152 | ->thenInvalid('Invalid "transport" parameter.') |
||
| 153 | ->end() |
||
| 154 | ->end() |
||
| 155 | ->end() |
||
| 156 | ->end() |
||
| 157 | ; |
||
| 158 | |||
| 159 | return $node; |
||
| 160 | } |
||
| 161 | } |
||
| 162 |
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: