Completed
Pull Request — master (#183)
by Luke
06:50 queued 02:52
created

IsReadable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B readLine() 0 29 7
isReadable() 0 1 ?
read() 0 1 ?
eof() 0 1 ?
A assertIsReadable() 0 6 2
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @copyright Copyright (c) 2018 Luke Visinoni
10
 * @author    Luke Visinoni <[email protected]>
11
 * @license   See LICENSE file (MIT license)
12
 */
13
namespace CSVelte\Traits;
14
15
use CSVelte\Exception\IOException;
16
17
/**
18
 * IO IsReadable Trait.
19
 *
20
 * Read methods shared between CSVelte\IO classes.
21
 */
22
trait IsReadable
23
{
24
    /**
25
     * Read single line.
26
     * Read the next line from the file (moving the internal pointer down a line).
27
     * Returns multiple lines if newline character(s) fall within a quoted string.
28
     *
29
     * @param string|array $eol       A string or array of strings to be used as EOL char/sequence
30
     * @param int          $maxLength Maximum number of bytes to return (line will be truncated to this -1 if set)
31
     *
32
     * @throws IOException
33
     *
34
     * @return string A single line read from the file.
35
     *
36
     * @todo Should this add a newline if maxlength is reached?
37
     * @todo I could actually buffer this by reading x chars at a time and doing
38
     *     the same thing with looping char by char if this is too IO intensive.
39
     */
40 6
    public function readLine($eol = PHP_EOL, $maxLength = null)
41
    {
42 6
        $size                     = 0;
43 6
        $buffer                   = false;
44 6
        if (!is_array($eol)) {
45 5
            $eol = [$eol];
46 5
        }
47 6
        while (!$this->eof()) {
48
            // Using a loose equality here to match on '' and false.
49 6
            if (null == ($byte = $this->read(1))) {
50
                return $buffer;
51
            }
52 6
            $buffer .= $byte;
53
            // Break when a new line is found or the max length - 1 is reached
54 6
            if (array_reduce($eol, function ($carry, $eol) use ($buffer) {
55 6
                    if (!$carry) {
56 6
                        $eollen = 0 - strlen($eol);
57
58 6
                        return substr($buffer, $eollen) === $eol;
59
                    }
60
61 1
                    return true;
62 6
                }, false) || ++$size === $maxLength - 1) {
63 6
                break;
64
            }
65 6
        }
66
67 6
        return $buffer;
68
    }
69
70
    abstract public function isReadable();
71
72
    abstract public function read($length);
73
74
    abstract public function eof();
75
76
    /**
77
     * Assert that this file/stream object is readable.
78
     *
79
     * @throws IOException
80
     */
81 19
    protected function assertIsReadable()
82
    {
83 19
        if (!$this->isReadable()) {
84 2
            throw new IOException('Stream not readable', IOException::ERR_NOT_READABLE);
85
        }
86 17
    }
87
}
88