Completed
Push — master ( 98adac...89edb3 )
by Christian
05:26
created

ClassUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 45
ccs 23
cts 24
cp 0.9583
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C findClassInFile() 0 35 12
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 1
    public static function findClassInFile($file)
27
    {
28 1
        $class = false;
29 1
        $namespace = false;
30 1
        $tokens = token_get_all(file_get_contents($file));
31 1
        for ($i = 0, $count = count($tokens); $i < $count; ++$i) {
32 1
            $token = $tokens[$i];
33
34 1
            if (!is_array($token)) {
35 1
                continue;
36
            }
37
38 1
            if (true === $class && T_STRING === $token[0]) {
39 1
                return $namespace.'\\'.$token[1];
40
            }
41
42 1
            if (true === $namespace && T_STRING === $token[0]) {
43 1
                $namespace = '';
44
                do {
45 1
                    $namespace .= $token[1];
46 1
                    $token = $tokens[++$i];
47 1
                } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
48 1
            }
49
50 1
            if (T_CLASS === $token[0]) {
51 1
                $class = true;
52 1
            }
53
54 1
            if (T_NAMESPACE === $token[0]) {
55 1
                $namespace = true;
56 1
            }
57 1
        }
58
59
        return false;
60
    }
61
}
62