| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright Andrea Heigl <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Licenses under the MIT-license. For details see the included file LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Callingallpapers\Subcommands\Sessionize\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Callingallpapers\Entity\Cfp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Callingallpapers\Service\ServiceContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Callingallpapers\Service\TimezoneService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use DateTimeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use DOMDocument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use DOMXPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | class EntryParser | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $cfp; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private $timezoneService; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $geolocationService; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(Cfp $cfp, ServiceContainer $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->cfp = $cfp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->timezoneService = $container->getTimezoneService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->geolocationService = $container->getGeolocationService(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function parse($uri) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $cfp = clone($this->cfp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $dom = new DOMDocument('1.0', 'UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $content = file_get_contents($uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $content = mb_convert_encoding($content, 'UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $content = preg_replace('/\<!DOCTYPE[^\>]*?\>/im', '', $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $dom->loadHTML( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 '<?xml version="1.0" encoding="UTF-8" ?>' . $content, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 LIBXML_NOBLANKS ^ LIBXML_NOERROR ^ LIBXML_NOENT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $dom->preserveWhiteSpace = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $xpath = new DOMXPath($dom); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $eventLocation = new Location(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $cfp->location = $eventLocation->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $location = $this->geolocationService->getLocationForAddress($cfp->location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $cfp->latitude = $location->getLatitude(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $cfp->longitude = $location->getLongitude(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $timezone = $this->timezoneService->getTimezoneForLocation($location->getLatitude(), $location->getLongitude()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } catch (\UnexpectedValueException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 error_log($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $cfp->timezone = $timezone; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $timezone = new DateTimeZone($cfp->timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $closingDateParser = new ClosingDate($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $cfp->dateEnd = $closingDateParser->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $descriptionParser = new Description(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $cfp->description = $descriptionParser->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $openingDateParser = new OpeningDate($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $cfp->dateStart = $openingDateParser->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } catch (Exception $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $cfp->uri = $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $confNameParser = new EventName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $cfp->conferenceName = $confNameParser->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $confUriParser = new EventUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $cfp->conferenceUri  = $confUriParser->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $eventStartDate = new EventStartDate($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $eventEndDate      = new EventEndDate($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } catch (InvalidArgumentException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $cfp->eventEndDate = $cfp->eventStartDate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $iconUri = new IconUri($uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $cfp->iconUri = $iconUri->parse($dom, $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return $cfp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                        
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: