FileLineIterator::__construct()   C
last analyzed

Complexity

Conditions 11
Paths 25

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 6.8569
c 0
b 0
f 0
cc 11
nc 25
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

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) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Stream\StreamInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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