Completed
Push — master ( d84f06...6f6abe )
by Shcherbak
02:36
created

ClassPattern   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 6 Features 3
Metric Value
wmc 8
c 9
b 6
f 3
lcom 1
cbo 3
dl 0
loc 77
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A nameIs() 0 4 1
A whereName() 0 4 1
A withName() 0 11 3
A __invoke() 0 15 2
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Pattern\Patterns;
4
5
  use Funivan\PhpTokenizer\QuerySequence\QuerySequence;
6
  use Funivan\PhpTokenizer\Strategy\QueryStrategy;
7
  use Funivan\PhpTokenizer\Strategy\Strict;
8
9
  /**
10
   * Pattern used to finding classes in tour source code
11
   *
12
   */
13
  class ClassPattern implements PatternInterface {
14
15
    /**
16
     * @var QueryStrategy
17
     */
18
    private $nameQuery = null;
19
20
21
    /**
22
     * By default we search for classes with any name
23
     */
24 32
    public function __construct() {
25 32
      $this->nameQuery = Strict::create()->valueLike('!.+!');
26 32
    }
27
28
29
    /**
30
     * @codeCoverageIgnore
31
     * @deprecated
32
     * @param string $name
33
     * @return $this
34
     */
35
    public function nameIs($name) {
36
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
37
      return $this->withName($name);
38
    }
39
40
41
    /**
42
     * @param QueryStrategy|string $name
43
     * @return $this
44
     */
45 14
    public function withName($name) {
46 14
      if (is_string($name)) {
47 8
        $this->nameQuery = Strict::create()->valueIs($name);
48 9
      } elseif ($name instanceof QueryStrategy) {
49 6
        $this->nameQuery = $name;
50 4
      } else {
51 3
        throw new \InvalidArgumentException('Expect string or QueryInterface');
52
      }
53
54 11
      return $this;
55
    }
56
57
58
    /**
59
     * @codeCoverageIgnore
60
     * @deprecated
61
     * @param QueryStrategy $strategy
62
     * @return $this
63
     */
64
    public function whereName(QueryStrategy $strategy) {
65
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
66
      return $this->withName($strategy);
67
    }
68
69
70
    /**
71
     * @inheritdoc
72
     */
73 29
    public function __invoke(QuerySequence $querySequence) {
74
75 29
      $querySequence->strict('class');
76 29
      $querySequence->strict(T_WHITESPACE);
77 29
      $querySequence->process($this->nameQuery);
78 29
      $startClassBody = $querySequence->search('{');
79 29
      $querySequence->moveToToken($startClassBody);
80 29
      $body = $querySequence->section('{', '}');
81
82 29
      if ($querySequence->isValid()) {
83 26
        return $body->extractItems(1, -1);
84
      }
85
86 29
      return null;
87
    }
88
89
  }