| Conditions | 2 |
| Paths | 1 |
| Total Lines | 113 |
| Code Lines | 69 |
| Lines | 15 |
| Ratio | 13.27 % |
| 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 |
||
| 55 | public function register(Container $pimple) |
||
| 56 | { |
||
| 57 | $pimple['open_platform.verify_ticket'] = function ($pimple) { |
||
| 58 | return new VerifyTicket( |
||
| 59 | $pimple['config']['open_platform']['app_id'], |
||
| 60 | $pimple['cache'] |
||
| 61 | ); |
||
| 62 | }; |
||
| 63 | |||
| 64 | View Code Duplication | $pimple['open_platform.access_token'] = function ($pimple) { |
|
|
|
|||
| 65 | return new AccessToken( |
||
| 66 | $pimple['config']['open_platform']['app_id'], |
||
| 67 | $pimple['config']['open_platform']['secret'], |
||
| 68 | $pimple['open_platform.verify_ticket'], |
||
| 69 | $pimple['cache'] |
||
| 70 | ); |
||
| 71 | }; |
||
| 72 | |||
| 73 | View Code Duplication | $pimple['open_platform.encryptor'] = function ($pimple) { |
|
| 74 | return new Encryptor( |
||
| 75 | $pimple['config']['open_platform']['app_id'], |
||
| 76 | $pimple['config']['open_platform']['token'], |
||
| 77 | $pimple['config']['open_platform']['aes_key'] |
||
| 78 | ); |
||
| 79 | }; |
||
| 80 | |||
| 81 | $pimple['open_platform'] = function ($pimple) { |
||
| 82 | return new OpenPlatform($pimple); |
||
| 83 | }; |
||
| 84 | |||
| 85 | $pimple['open_platform.server'] = function ($pimple) { |
||
| 86 | $server = new Guard($pimple['config']['open_platform']['token']); |
||
| 87 | $server->debug($pimple['config']['debug']); |
||
| 88 | $server->setEncryptor($pimple['open_platform.encryptor']); |
||
| 89 | $server->setHandlers([ |
||
| 90 | Guard::EVENT_AUTHORIZED => $pimple['open_platform.handlers.authorized'], |
||
| 91 | Guard::EVENT_UNAUTHORIZED => $pimple['open_platform.handlers.unauthorized'], |
||
| 92 | Guard::EVENT_UPDATE_AUTHORIZED => $pimple['open_platform.handlers.updateauthorized'], |
||
| 93 | Guard::EVENT_COMPONENT_VERIFY_TICKET => $pimple['open_platform.handlers.component_verify_ticket'], |
||
| 94 | ]); |
||
| 95 | |||
| 96 | return $server; |
||
| 97 | }; |
||
| 98 | |||
| 99 | $pimple['open_platform.pre_auth'] = $pimple['open_platform.pre_authorization'] = function ($pimple) { |
||
| 100 | return new PreAuthorization( |
||
| 101 | $pimple['open_platform.access_token'], |
||
| 102 | $pimple['config']['open_platform'] |
||
| 103 | ); |
||
| 104 | }; |
||
| 105 | |||
| 106 | $pimple['open_platform.api'] = function ($pimple) { |
||
| 107 | return new BaseApi( |
||
| 108 | $pimple['open_platform.access_token'], |
||
| 109 | $pimple['config']['open_platform'] |
||
| 110 | ); |
||
| 111 | }; |
||
| 112 | |||
| 113 | $pimple['open_platform.authorization'] = function ($pimple) { |
||
| 114 | return new Authorization( |
||
| 115 | $pimple['open_platform.api'], |
||
| 116 | $pimple['config']['open_platform']['app_id'], |
||
| 117 | $pimple['cache'] |
||
| 118 | ); |
||
| 119 | }; |
||
| 120 | |||
| 121 | $pimple['open_platform.authorizer_token'] = function ($pimple) { |
||
| 122 | return new AuthorizerAccessToken( |
||
| 123 | $pimple['config']['open_platform']['app_id'], |
||
| 124 | $pimple['open_platform.authorization'] |
||
| 125 | ); |
||
| 126 | }; |
||
| 127 | |||
| 128 | // Authorization events handlers. |
||
| 129 | $pimple['open_platform.handlers.component_verify_ticket'] = function ($pimple) { |
||
| 130 | return new EventHandlers\ComponentVerifyTicket($pimple['open_platform.verify_ticket']); |
||
| 131 | }; |
||
| 132 | $pimple['open_platform.handlers.authorized'] = function ($pimple) { |
||
| 133 | return new EventHandlers\Authorized($pimple['open_platform.authorization']); |
||
| 134 | }; |
||
| 135 | $pimple['open_platform.handlers.updateauthorized'] = function ($pimple) { |
||
| 136 | return new EventHandlers\UpdateAuthorized($pimple['open_platform.authorization']); |
||
| 137 | }; |
||
| 138 | $pimple['open_platform.handlers.unauthorized'] = function ($pimple) { |
||
| 139 | return new EventHandlers\Unauthorized($pimple['open_platform.authorization']); |
||
| 140 | }; |
||
| 141 | |||
| 142 | $pimple['open_platform.app'] = function ($pimple) { |
||
| 143 | return new Application($pimple['config']->toArray()); |
||
| 144 | }; |
||
| 145 | |||
| 146 | // OAuth for OpenPlatform. |
||
| 147 | $pimple['open_platform.oauth'] = function ($pimple) { |
||
| 148 | $callback = $this->prepareCallbackUrl($pimple); |
||
| 149 | $scopes = $pimple['config']->get('open_platform.oauth.scopes', []); |
||
| 150 | $socialite = (new Socialite([ |
||
| 151 | 'wechat_open' => [ |
||
| 152 | 'client_id' => $pimple['open_platform.authorization']->getAuthorizerAppId(), |
||
| 153 | 'client_secret' => [ |
||
| 154 | $pimple['open_platform.access_token']->getAppId(), |
||
| 155 | $pimple['open_platform.access_token']->getToken(), |
||
| 156 | ], |
||
| 157 | 'redirect' => $callback, |
||
| 158 | ], |
||
| 159 | ]))->driver('wechat_open'); |
||
| 160 | |||
| 161 | if (!empty($scopes)) { |
||
| 162 | $socialite->scopes($scopes); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $socialite; |
||
| 166 | }; |
||
| 167 | } |
||
| 168 | |||
| 187 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.