Completed
Push — master ( 5db41f...c1280c )
by Daniel
01:31
created

ClassScanner   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getClassNameFromFile() 0 52 15
1
<?php
2
3
namespace Phpactor\ClassFileConverter\Adapter\Simple;
4
5
/**
6
 * Return the class name from a file.
7
 *
8
 * Based on http://stackoverflow.com/questions/7153000/get-class-name-from-file
9
 */
10
class ClassScanner
11
{
12
    public function getClassNameFromFile($file)
13
    {
14
        $fp = fopen($file, 'r');
15
16
        $class = $namespace = $buffer = '';
17
        $i = 0;
18
19
        while (!$class) {
20
            if (feof($fp)) {
21
                break;
22
            }
23
24
            // Read entire lines to prevent keyword truncation
25
            for ($line = 0; $line <= 20; $line++) {
26
                $buffer .= fgets($fp);
27
            }
28
            $tokens = @token_get_all($buffer);
29
30
            if (strpos($buffer, '{') === false) {
31
                continue;
32
            }
33
34
            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...
35
                if ($tokens[$i][0] === \T_NAMESPACE) {
36
                    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...
37
                        if ($tokens[$j][0] === T_STRING) {
38
                            $namespace .= '\\' . $tokens[$j][1];
39
                        } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
40
                            break;
41
                        }
42
                    }
43
                }
44
45
                if ($tokens[$i][0] === \T_CLASS) {
46
                    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...
47
                        if ($tokens[$j][0] === \T_STRING) {
48
                            $class = $tokens[$i + 2][1];
49
                            break 2;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
56
        if (!trim($class)) {
57
            return;
58
        }
59
60
        fclose($fp);
61
62
        return ltrim($namespace . '\\' . $class, '\\');
63
    }
64
}
65