| Conditions | 33 | 
| Paths | 2088 | 
| Total Lines | 216 | 
| Lines | 14 | 
| Ratio | 6.48 % | 
| 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  | 
            ||
| 93 | public function editGame($templatePath, $formId)  | 
            ||
| 94 |     { | 
            ||
| 95 | // We try to get FB pages from the logged in user  | 
            ||
| 96 |         $session = new Container('facebook'); | 
            ||
| 97 |         $config = $this->getServiceLocator()->get('config'); | 
            ||
| 98 | $appsArray = [];  | 
            ||
| 99 | $platformFbAppId = '';  | 
            ||
| 100 | |||
| 101 | View Code Duplication |         if (isset($config['facebook'])) { | 
            |
| 102 | $platformFbAppId = $config['facebook']['fb_appid'];  | 
            ||
| 103 | $platformFbAppSecret = $config['facebook']['fb_secret'];  | 
            ||
| 104 | $fbPage = $config['facebook']['fb_page'];  | 
            ||
| 105 | }  | 
            ||
| 106 | $fb = new \Facebook\Facebook([  | 
            ||
| 107 | 'app_id' => $platformFbAppId,  | 
            ||
| 108 | 'app_secret' => $platformFbAppSecret,  | 
            ||
| 109 | 'default_graph_version' => 'v3.1',  | 
            ||
| 110 | //'cookie' => false,  | 
            ||
| 111 |             //'default_access_token' => '{access-token}', // optional | 
            ||
| 112 | ]);  | 
            ||
| 113 | |||
| 114 | $helper = $fb->getRedirectLoginHelper();  | 
            ||
| 115 |         $fb_args_param = array('req_perms' => 'manage_pages,publish_pages'); | 
            ||
| 116 | $fb_login_url = $helper->getLoginUrl($this->url()->fromRoute(  | 
            ||
| 117 | 'admin/playgroundgame/list',  | 
            ||
| 118 | array(),  | 
            ||
| 119 |             array('force_canonical' => true)), $fb_args_param); | 
            ||
| 120 | $accessToken = $helper->getAccessToken();  | 
            ||
| 121 | |||
| 122 |         if (isset($accessToken) || $session->offsetExists('fb_token')) { | 
            ||
| 123 |             if (isset($accessToken)) { | 
            ||
| 124 |                 $session->offsetSet('fb_token', $accessToken); | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | // checking if user access token is not valid then ask user to login again  | 
            ||
| 128 |             $debugToken = $fb->get('/debug_token?input_token='. $session->offsetGet('fb_token'), $platformFbAppId . '|' . $platformFbAppSecret) | 
            ||
| 129 | ->getGraphNode()  | 
            ||
| 130 | ->asArray();  | 
            ||
| 131 |             if (isset($debugToken['error']['code'])) { | 
            ||
| 132 |                 $session->offsetUnset('fb_token'); | 
            ||
| 133 |             } else { | 
            ||
| 134 | // setting default user access token for future requests  | 
            ||
| 135 |                 $fb->setDefaultAccessToken($session->offsetGet('fb_token')); | 
            ||
| 136 |                 $pages = $fb->get('/me/accounts') | 
            ||
| 137 | ->getGraphEdge()  | 
            ||
| 138 | ->asArray();  | 
            ||
| 139 | |||
| 140 |                 foreach ($pages as $key) { | 
            ||
| 141 | $app_label = '';  | 
            ||
| 142 |                     if (isset($key['name'])) { | 
            ||
| 143 | $app_label .= $key['name'];  | 
            ||
| 144 | }  | 
            ||
| 145 |                     if (isset($key['id'])) { | 
            ||
| 146 |                         $app_label .= ' ('.$key['id'].')'; | 
            ||
| 147 | }  | 
            ||
| 148 | $appsArray[$key['id']] = $app_label;  | 
            ||
| 149 | }  | 
            ||
| 150 | $fb_login_url = '';  | 
            ||
| 151 | |||
| 152 |                 if ($this->getRequest()->isPost()) { | 
            ||
| 153 | $data = array_replace_recursive(  | 
            ||
| 154 | $this->getRequest()->getPost()->toArray(),  | 
            ||
| 155 | $this->getRequest()->getFiles()->toArray()  | 
            ||
| 156 | );  | 
            ||
| 157 | // Removing a previously page tab set on this game  | 
            ||
| 158 | if(  | 
            ||
| 159 | $this->game &&  | 
            ||
| 160 | !empty($this->game->getFbPageId()) &&  | 
            ||
| 161 | !empty($this->game->getFbAppId()) &&  | 
            ||
| 162 | (  | 
            ||
| 163 | $this->game->getFbPageId() !== $data['fbPageId'] ||  | 
            ||
| 164 | $this->game->getFbAppId() !== $data['fbAppId']  | 
            ||
| 165 | )  | 
            ||
| 166 |                     ) { | 
            ||
| 167 |                         $oldPage = $fb->get('/' . $this->game->getFbPageId() . '?fields=access_token,name,id') | 
            ||
| 168 | ->getGraphNode()  | 
            ||
| 169 | ->asArray();  | 
            ||
| 170 | $removeTab = $fb->delete(  | 
            ||
| 171 | '/' . $this->game->getFbPageId() . '/tabs',  | 
            ||
| 172 | [  | 
            ||
| 173 | 'tab' => 'app_'.$this->game->getFbAppId(),  | 
            ||
| 174 | ],  | 
            ||
| 175 | $oldPage['access_token']  | 
            ||
| 176 | )  | 
            ||
| 177 | ->getGraphNode()  | 
            ||
| 178 | ->asArray();  | 
            ||
| 179 | }  | 
            ||
| 180 | }  | 
            ||
| 181 | }  | 
            ||
| 182 | }  | 
            ||
| 183 | |||
| 184 | $viewModel = new ViewModel();  | 
            ||
| 185 | $viewModel->setTemplate($templatePath);  | 
            ||
| 186 | |||
| 187 | $gameForm = new ViewModel();  | 
            ||
| 188 |         $gameForm->setTemplate('playground-game/game/game-form'); | 
            ||
| 189 | |||
| 190 | $form = $this->getServiceLocator()->get($formId);  | 
            ||
| 191 | $form->setAttribute(  | 
            ||
| 192 | 'action',  | 
            ||
| 193 | $this->url()->fromRoute(  | 
            ||
| 194 | 'admin/playgroundgame/edit-' . $this->game->getClassType(),  | 
            ||
| 195 |                 array('gameId' => $this->game->getId()) | 
            ||
| 196 | )  | 
            ||
| 197 | );  | 
            ||
| 198 |         $form->setAttribute('method', 'post'); | 
            ||
| 199 | |||
| 200 |         $pageIds = $form->get('fbPageId')->getOption('value_options'); | 
            ||
| 201 |         foreach($appsArray as $k => $v) { | 
            ||
| 202 | $pageIds[$k] = $v;  | 
            ||
| 203 | }  | 
            ||
| 204 |         $form->get('fbPageId')->setAttribute('options', $pageIds); | 
            ||
| 205 | |||
| 206 |         //if($form->get('fbAppId')->getValue() == '') { | 
            ||
| 207 |             $form->get('fbAppId')->setValue($platformFbAppId); | 
            ||
| 208 | //}  | 
            ||
| 209 | |||
| 210 |         // if ($this->game->getFbAppId()) { | 
            ||
| 211 |         //     $data['fbAppId'] = $form->get('fbAppId')->getOption('value_options'); | 
            ||
| 212 | // $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId();  | 
            ||
| 213 |         //     $form->get('fbAppId')->setAttribute('options', $appIds); | 
            ||
| 214 | // }  | 
            ||
| 215 | |||
| 216 | $gameOptions = $this->getAdminGameService()->getOptions();  | 
            ||
| 217 | $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css';  | 
            ||
| 218 | View Code Duplication |         if (is_file($gameStylesheet)) { | 
            |
| 219 |             $values = $form->get('stylesheet')->getValueOptions(); | 
            ||
| 220 | $values[$gameStylesheet] = 'Style personnalisé de ce jeu';  | 
            ||
| 221 | |||
| 222 |             $form->get('stylesheet')->setAttribute('options', $values); | 
            ||
| 223 | }  | 
            ||
| 224 | |||
| 225 | $form->bind($this->game);  | 
            ||
| 226 | |||
| 227 |         if ($this->getRequest()->isPost()) { | 
            ||
| 228 | $data = array_replace_recursive(  | 
            ||
| 229 | $this->getRequest()->getPost()->toArray(),  | 
            ||
| 230 | $this->getRequest()->getFiles()->toArray()  | 
            ||
| 231 | );  | 
            ||
| 232 |             if (empty($data['prizes'])) { | 
            ||
| 233 | $data['prizes'] = array();  | 
            ||
| 234 | }  | 
            ||
| 235 | View Code Duplication |             if (isset($data['drawDate']) && $data['drawDate']) { | 
            |
| 236 |                 $data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); | 
            ||
| 237 | }  | 
            ||
| 238 | $game = $this->getAdminGameService()->createOrUpdate($data, $this->game, $formId);  | 
            ||
| 239 | |||
| 240 |             if ($game) { | 
            ||
| 241 | // Let's record the FB page tab if it is configured  | 
            ||
| 242 |                 if ($session->offsetExists('fb_token')) {         | 
            ||
| 243 | // adding page tab to selected page using page access token  | 
            ||
| 244 |                     if (!empty($data['fbPageId']) && !empty($data['fbAppId'])) { | 
            ||
| 245 |                         $page = $fb->get('/' . $data['fbPageId'] . '?fields=access_token,name,id') | 
            ||
| 246 | ->getGraphNode()  | 
            ||
| 247 | ->asArray();  | 
            ||
| 248 | |||
| 249 |                         try { | 
            ||
| 250 | $addTab = $fb->post(  | 
            ||
| 251 | '/' . $page['id'] . '/tabs',  | 
            ||
| 252 | [  | 
            ||
| 253 | 'app_id' => $data['fbAppId'],  | 
            ||
| 254 | 'custom_name' => (!empty($data['fbPageTabTitle'])) ? $data['fbPageTabTitle'] : $data['title'],  | 
            ||
| 255 | 'custom_image_url' => ($game->getFbPageTabImage() !== '') ?  | 
            ||
| 256 |                                         $this->getAdminGameService()->getServiceManager()->get('ViewRenderer')->url( | 
            ||
| 257 | 'frontend',  | 
            ||
| 258 | array(),  | 
            ||
| 259 |                                             array('force_canonical' => true) | 
            ||
| 260 | ).$game->getFbPageTabImage() :  | 
            ||
| 261 | null,  | 
            ||
| 262 | 'position' => (!empty($data['fbPageTabPosition'])) ? $data['fbPageTabPosition'] : 99  | 
            ||
| 263 | ],  | 
            ||
| 264 | $page['access_token'])  | 
            ||
| 265 | ->getGraphNode()  | 
            ||
| 266 | ->asArray();  | 
            ||
| 267 |                         } catch (\Exception $e) { | 
            ||
| 268 | // (#324) Missing or invalid image file  | 
            ||
| 269 |                             if($e->getCode() == '324') { | 
            ||
| 270 |                                 try { | 
            ||
| 271 | $addTab = $fb->post(  | 
            ||
| 272 | '/' . $page['id'] . '/tabs',  | 
            ||
| 273 | [  | 
            ||
| 274 | 'app_id' => $data['fbAppId'],  | 
            ||
| 275 | 'custom_name' => (!empty($data['fbPageTabTitle'])) ? $data['fbPageTabTitle'] : $data['title'],  | 
            ||
| 276 | 'position' => (!empty($data['fbPageTabPosition'])) ? $data['fbPageTabPosition'] : 99  | 
            ||
| 277 | ],  | 
            ||
| 278 | $page['access_token'])  | 
            ||
| 279 | ->getGraphNode()  | 
            ||
| 280 | ->asArray();  | 
            ||
| 281 |                                 } catch (\Exception $e) { | 
            ||
| 282 | throw $e;  | 
            ||
| 283 | }  | 
            ||
| 284 | }  | 
            ||
| 285 | }  | 
            ||
| 286 | }  | 
            ||
| 287 | }  | 
            ||
| 288 |                 return $this->redirect()->toRoute('admin/playgroundgame/list'); | 
            ||
| 289 | }  | 
            ||
| 290 | }  | 
            ||
| 291 | |||
| 292 | $gameForm->setVariables(  | 
            ||
| 293 | array(  | 
            ||
| 294 | 'platform_fb_app_id' => $platformFbAppId,  | 
            ||
| 295 | 'fb_login_url' => $fb_login_url,  | 
            ||
| 296 | 'form' => $form,  | 
            ||
| 297 | 'game' => $this->game  | 
            ||
| 298 | )  | 
            ||
| 299 | );  | 
            ||
| 300 | $viewModel->addChild($gameForm, 'game_form');  | 
            ||
| 301 | |||
| 302 | return $viewModel->setVariables(  | 
            ||
| 303 | array(  | 
            ||
| 304 | 'form' => $form,  | 
            ||
| 305 | 'title' => 'Edit this game',  | 
            ||
| 306 | )  | 
            ||
| 307 | );  | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 625 | 
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..