| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Graze\DataFile\IO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Graze\DataFile\Format\FormatAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Graze\DataFile\Format\FormatInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Graze\DataFile\Format\Parser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Graze\DataFile\Format\Parser\ParserFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Graze\DataFile\Helper\OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Graze\DataFile\Node\FileNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Graze\DataFile\Node\NodeStreamInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GuzzleHttp\Psr7\Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Iterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Psr\Log\LoggerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class FileReader implements ReaderInterface, LoggerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var FileNodeInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var FormatInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var StreamReader */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * FileReader constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param FileNodeInterface      $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param FormatInterface|null   $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param ParserFactoryInterface $parserFactory | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         FileNodeInterface $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         FormatInterface $format = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ParserFactoryInterface $parserFactory = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 7 |  |         $this->file = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |         $this->format = $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 7 |  |         if ($this->file instanceof NodeStreamInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |             $stream = $this->file->getStream('r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |             $stream = new Stream($this->file->readStream()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 7 |  |         if (is_null($this->format) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 7 |  |             && $file instanceof FormatAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 7 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             $this->format = $file->getFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |         if (is_null($this->format)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             throw new InvalidArgumentException("No format could be determined from \$file or \$format"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |         $factory = $parserFactory ?: new ParserFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  |         $parser = $factory->getParser($this->format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  |         $this->reader = new StreamReader($stream, $parser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Fetch the next row from a result set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param callable|null $callable a callable function to be applied to each Iterator item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return Iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |     public function fetch(callable $callable = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         return $this->reader->fetch($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Returns a sequential array of all items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * The callable function will be applied to each Iterator item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param callable|null $callable a callable function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 86 | 1 |  |     public function fetchAll(callable $callable = null) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 88 | 1 |  |         return $this->reader->fetchAll($callable); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.