Completed
Push — master ( 00c7cd...599862 )
by Daniel
02:16
created

SimpleFileToClass::fileToClassCandidates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Phpactor\ClassFileConverter\Adapter\Simple;
4
5
use Phpactor\ClassFileConverter\Domain\FileToClass;
6
use Phpactor\ClassFileConverter\Domain\ClassNameCandidates;
7
use Phpactor\ClassFileConverter\Domain\FilePath;
8
use Phpactor\ClassFileConverter\Domain\ClassName;
9
10
class SimpleFileToClass implements FileToClass
11
{
12
    public function fileToClassCandidates(FilePath $filePath): ClassNameCandidates
13
    {
14
        $classNames = [];
15
16
        $className = $this->getClassNameFromFile($filePath->__toString());
17
18
        if ($className) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $className of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
19
            $classNames[] = ClassName::fromString($className);
20
        }
21
22
        return ClassNameCandidates::fromClassNames($classNames);
23
    }
24
25
    /**
26
     * Return the class name from a file.
27
     *
28
     * Taken from http://stackoverflow.com/questions/7153000/get-class-name-from-file
29
     *
30
     * @param string $file
31
     *
32
     * @return string
33
     */
34
    private function getClassNameFromFile($file)
35
    {
36
        $fp = fopen($file, 'r');
37
38
        $class = $namespace = $buffer = '';
39
        $i = 0;
40
41
        while (!$class) {
42
            if (feof($fp)) {
43
                break;
44
            }
45
46
            // Read entire lines to prevent keyword truncation
47
            for ($line = 0; $line <= 20; $line++) {
48
                $buffer .= fgets($fp);
49
            }
50
            $tokens = @token_get_all($buffer);
51
52
            if (strpos($buffer, '{') === false) {
53
                continue;
54
            }
55
56
            for (; $i < count($tokens); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
57
                if ($tokens[$i][0] === T_NAMESPACE) {
58
                    for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
59
                        if ($tokens[$j][0] === T_STRING) {
60
                            $namespace .= '\\' . $tokens[$j][1];
61
                        } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
62
                            break;
63
                        }
64
                    }
65
                }
66
67
                if ($tokens[$i][0] === T_CLASS) {
68
                    for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
69
                        if ($tokens[$j][0] === T_STRING) {
70
                            $class = $tokens[$i + 2][1];
71
                            break 2;
72
                        }
73
                    }
74
                }
75
            }
76
        }
77
78
        if (!trim($class)) {
79
            return;
80
        }
81
82
        fclose($fp);
83
84
        return ltrim($namespace . '\\' . $class, '\\');
85
    }
86
}
87