ClassScanner   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 0
dl 0
loc 67
rs 10
c 0
b 0
f 0

1 Method

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