ReadsModel::readModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 10
cc 2
nc 2
nop 1
crap 2.0185
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