Tokenlist::getClassName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
/**
3
 * Copyright (c)2014-2014 heiglandreas
4
 * 
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 * 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category 
24
 * @author    Andreas Heigl<[email protected]>
25
 * @copyright ©2014-2014 Andreas Heigl
26
 * @license   http://www.opesource.org/licenses/mit-license.php MIT-License
27
 * @version   0.0
28
 * @since     17.11.14
29
 * @link      https://github.com/heiglandreas/
30
 */
31
32
namespace Org_Heigl\FileFinder\Service;
33
34
35
class Tokenlist 
36
{
37
38
    /**
39
     * @var array $tokenlist
40
     */
41
    protected $tokenlist;
42
43
    /**
44
     * Create a tokenlist based on the given string
45
     *
46
     * @param string $content
47
     */
48 13
    public function __construct($content)
49
    {
50 13
        $this->tokenlist = token_get_all($content);
51 13
    }
52
53
    /**
54
     * Get the first classname
55
     *
56
     * @return string
57
     */
58 11
    public function getClassName()
59
    {
60 11
        foreach ($this->tokenlist as $key => $token) {
61 11
            if (T_CLASS === $token[0]) {
62 11
                return $this->tokenlist[$key + 2][1];
63
            }
64
        }
65
66 3
        return '';
67
    }
68
69
    /**
70
     * Get the first namespace of the given content
71
     *
72
     * @return array
73
     */
74 11
    public function getNamespace()
75
    {
76 11
        $class       = array();
77 11
        $inNamespace = false;
78
79 11
        foreach ($this->tokenlist as $key => $token) {
80 11
            if (T_NAMESPACE === $token[0]) {
81 5
                $inNamespace = true;
82 5
                continue;
83
            }
84 11
            if (T_STRING === $token[0] && $inNamespace) {
85 5
                $class[] = $token[1];
86
87
            }
88 11
            if (';' === $token && $inNamespace) {
89 11
                return $class;
90
            }
91
        }
92
93 8
        return array();
94
    }
95
}
96