| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PRReviewWatcher\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Silex\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ApiController | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |     public function createCheckListAction(Request $request, Application $app) | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         $repoHook      = $request->request->get('pull_request')['head']['repo']['full_name']; | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         $branchHook    = $request->request->get('pull_request')['base']['ref']; | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |         $numberHook    = $request->request->get('number'); | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |         $testOpened    = $request->request->get('action'); | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |         $testStateOpen = $request->request->get('pull_request')['state']; | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |         //Creation of a pull request and not an issue. | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         if (($testOpened == 'opened') && ($testStateOpen == 'open')) { | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |             $branch = $app['project_repository']->findBranch($repoHook); | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |             if (in_array('all', $branch)) { | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |                 $comment = $app['project_repository']->findComment($repoHook, null); | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |                 $id = $app['project_repository']->findId($repoHook, null); | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |                 $token  = $app['credential_repository']->findToken($repoHook, null); | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |             elseif (in_array($branchHook, $branch)){ | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |                 $comment = $app['project_repository']->findComment($repoHook, $branchHook); | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |                 $id = $app['project_repository']->findId($repoHook, $branchHook); | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |                 $token  = $app['credential_repository']->findToken($repoHook, $branchHook); | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |             if (($comment != null) && ($token != null)) { | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |                 $content = [ | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |                     'auth' => [ | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |                         'token', | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |                         $token, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |                     ] | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |                     , | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |                     'json' => [ | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |                         'body' => $comment, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |                     ], | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |                 ]; | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |                 $url    = 'https://api.github.com/repos/' . $repoHook . '/issues/' . $numberHook . '/comments'; | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 $client = new Client(); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |                 $client->post($url, $content); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                 $app['project_repository']->incrementNumber($id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |                 return $app->json($content, 201); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                        
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: