1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace itertools; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class FileLineIterator extends TakeWhileIterator |
9
|
|
|
{ |
10
|
|
|
protected $fileHandle; |
11
|
|
|
protected $closeFileHandleOnDestruct; |
12
|
|
|
protected $guzzleStream; |
13
|
|
|
|
14
|
|
|
public function __construct($file, $options = array()) |
15
|
|
|
{ |
16
|
|
|
$defaultOptions = array( |
17
|
|
|
'includeWhitespace' => false, |
18
|
|
|
'fromEncoding' => null, |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
$unknownOptions = array_diff(array_keys($options), array_keys($defaultOptions)); |
22
|
|
|
if (count($unknownOptions) != 0) { |
23
|
|
|
throw new InvalidArgumentException('Unknown options specified: ' . implode(', ', $unknownOptions)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$options = (object) array_merge($defaultOptions, $options); |
27
|
|
|
|
28
|
|
|
if ($file instanceof \GuzzleHttp\Stream\StreamInterface) { |
29
|
|
|
$this->guzzleStream = $file; |
30
|
|
|
$file = \GuzzleHttp\Stream\GuzzleStreamWrapper::getResource($file); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($file instanceof \Guzzle\Stream\StreamInterface) { |
|
|
|
|
34
|
|
|
$this->guzzleStream = $file; |
35
|
|
|
$file = $file->getStream(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (is_resource($file)) { |
39
|
|
|
$this->fileHandle = $file; |
40
|
|
|
$this->closeFileHandleOnDestruct = false; |
41
|
|
|
if(null !== $options->fromEncoding) { |
42
|
|
|
throw new Exception('Source encoding can only be specified if constructed with file path'); |
43
|
|
|
} |
44
|
|
|
} else if (is_string($file)) { |
45
|
|
|
$this->fileHandle = @fopen($file, 'r'); |
46
|
|
|
if ($this->fileHandle === false) { |
47
|
|
|
throw new InvalidArgumentException("Could not open file with path: '$file'"); |
48
|
|
|
} |
49
|
|
|
if (null !== $options->fromEncoding) { |
50
|
|
|
stream_filter_append($this->fileHandle, 'convert.iconv.' . $options->fromEncoding . '/UTF-8'); |
51
|
|
|
} |
52
|
|
|
$this->closeFileHandleOnDestruct = true; |
53
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
throw new InvalidArgumentException('You must provide either a stream or filename to the file line iterator, you provided a ' . gettype($file)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$fileHandle = $this->fileHandle; |
59
|
|
|
$lineIterator = new CallbackIterator(function () use ($fileHandle, $options) { |
60
|
|
|
$line = fgets($fileHandle); |
61
|
|
|
if ($line !== false && !$options->includeWhitespace) { |
62
|
|
|
$line = rtrim($line, "\r\n"); |
63
|
|
|
} |
64
|
|
|
return $line; |
65
|
|
|
}); |
66
|
|
|
parent::__construct($lineIterator, function ($line) { return $line !== false; }); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function __destruct() |
70
|
|
|
{ |
71
|
|
|
if ($this->fileHandle !== null && $this->closeFileHandleOnDestruct) { |
72
|
|
|
fclose($this->fileHandle); |
73
|
|
|
$this->fileHandle = null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.