ReadsModel   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 2
b 0
f 0
dl 0
loc 33
ccs 9
cts 11
cp 0.8182
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readFirstLineOfMethod() 0 7 1
A readModel() 0 10 2
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