ReadsModel::readFirstLineOfMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Cerbero\EloquentInspector\Concerns;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use SplFileObject;
8
9
/**
10
 * The trait to read a model file.
11
 *
12
 */
13
trait ReadsModel
14
{
15
    /**
16
     * Lazily read the model line by line.
17
     *
18
     * @param string $model
19
     * @return iterable
20
     */
21 1
    protected function readModel(string $model): iterable
22
    {
23 1
        $filename = (new ReflectionClass($model))->getFileName();
24 1
        $handle = fopen($filename, 'rb');
25
26 1
        while (($line = fgets($handle, 1024)) !== false) {
27 1
            yield $line;
28
        }
29
30
        fclose($handle);
31
    }
32
33
    /**
34
     * Lazily read the first line of the given model method
35
     *
36
     * @param ReflectionMethod $method
37
     * @return string
38
     */
39 1
    protected function readFirstLineOfMethod(ReflectionMethod $method): string
40
    {
41 1
        $file = new SplFileObject($method->getFileName(), 'rb');
42
43 1
        $file->seek($method->getStartLine() + 1);
44
45 1
        return $file->fgets();
46
    }
47
}
48