Completed
Push — master ( f9a35e...e44c95 )
by Guilh
08:02 queued 04:27
created

ClassUtils::findClassInFile()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 24
cts 24
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 21
nc 11
nop 1
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Routing\Loader;
13
14
/**
15
 * @internal
16
 */
17
class ClassUtils
18
{
19
    /**
20
     * Returns the full class name for the first class in the file.
21
     *
22
     * @param string $file A PHP file path
23
     *
24
     * @return string|false Full class name if found, false otherwise
25
     */
26 2
    public static function findClassInFile($file)
27
    {
28 2
        $class = false;
29 2
        $namespace = false;
30 2
        $tokens = token_get_all(file_get_contents($file));
31 2
        for ($i = 0, $count = count($tokens); $i < $count; ++$i) {
32 2
            $token = $tokens[$i];
33
34 2
            if (!is_array($token)) {
35 2
                continue;
36
            }
37
38 2
            if (true === $class && T_STRING === $token[0]) {
39 2
                return $namespace.'\\'.$token[1];
40
            }
41
42 2
            if (true === $namespace && T_STRING === $token[0]) {
43 2
                $namespace = '';
44
                do {
45 2
                    $namespace .= $token[1];
46 2
                    $token = $tokens[++$i];
47 2
                } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
48 2
            }
49
50 2
            if (T_CLASS === $token[0]) {
51 2
                $class = true;
52 2
            }
53
54 2
            if (T_NAMESPACE === $token[0]) {
55 2
                $namespace = true;
56 2
            }
57 2
        }
58
59 1
        return false;
60
    }
61
}
62