Completed
Pull Request — 2.x (#2280)
by
unknown
09:25 queued 07:49
created

ClassUtils::findClassInFile()   C

Complexity

Conditions 13
Paths 22

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13.0139

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 22
cts 23
cp 0.9565
rs 6.6166
c 0
b 0
f 0
cc 13
nc 22
nop 1
crap 13.0139

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
 * @deprecated
18
 */
19
class ClassUtils
20
{
21 2
    public static function findClassInFile(string $file): ?string
22
    {
23 2
        $class = false;
24 2
        $namespace = false;
25 2
        $tokens = token_get_all(file_get_contents($file));
26
27 2
        if (defined('T_NAME_QUALIFIED')) {
28
            $namespaceToken = T_NAME_QUALIFIED;
29
        } else {
30 2
            $namespaceToken = T_STRING;
31
        }
32
33 2
        for ($i = 0, $count = count($tokens); $i < $count; ++$i) {
34 2
            $token = $tokens[$i];
35
36 2
            if (!is_array($token)) {
37 2
                continue;
38
            }
39
40 2
            if (true === $class && T_STRING === $token[0]) {
41 2
                return $namespace.'\\'.$token[1];
42
            }
43
44 2
            if (true === $namespace && $namespaceToken === $token[0]) {
45 2
                $namespace = '';
46
                do {
47 2
                    $namespace .= $token[1];
48 2
                    $token = $tokens[++$i];
49 2
                } while ($i < $count && is_array($token) && in_array($token[0], [T_NS_SEPARATOR, $namespaceToken]));
50
            }
51
52 2
            if (T_CLASS === $token[0]) {
53 2
                $class = true;
54
            }
55
56 2
            if (T_NAMESPACE === $token[0]) {
57 2
                $namespace = true;
58
            }
59
        }
60
61 1
        return null;
62
    }
63
}
64