Completed
Push — master ( 54f175...7d7c0f )
by Shcherbak
04:05 queued 01:51
created

ClassPattern::outputBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Pattern\Patterns;
4
5
  use Funivan\PhpTokenizer\QuerySequence\QuerySequence;
6
  use Funivan\PhpTokenizer\Strategy\Possible;
7
  use Funivan\PhpTokenizer\Strategy\QueryStrategy;
8
  use Funivan\PhpTokenizer\Strategy\Strict;
9
10
  /**
11
   * Pattern used to finding classes in tour source code
12
   *
13
   */
14
  class ClassPattern implements PatternInterface {
15
16
    /**
17
     * Result of this pattern will be body of the class
18
     */
19
    const OUTPUT_BODY = 1;
20
21
    /**
22
     * Result of this pattern will be full class
23
     */
24
    const OUTPUT_FULL = 2;
25
26
27
    /**
28
     * @var QueryStrategy
29
     */
30
    private $nameQuery = null;
31
32
    /**
33
     * @var int
34
     */
35
    private $outputType = self::OUTPUT_BODY;
36
37
38
    /**
39
     * By default we search for classes with any name
40
     */
41 40
    public function __construct() {
42 28
      $this->nameQuery = Strict::create()->valueLike('!.+!');
43 40
    }
44
45
46
    /**
47
     * @codeCoverageIgnore
48
     * @deprecated
49
     * @param string $name
50
     * @return $this
51
     */
52
    public function nameIs($name) {
53
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
54
      return $this->withName($name);
55
    }
56
57
58
    /**
59
     * @param QueryStrategy|string $name
60
     * @return $this
61
     */
62 12
    public function withName($name) {
63 10
      if (is_string($name)) {
64 6
        $this->nameQuery = Strict::create()->valueIs($name);
65 7
      } elseif ($name instanceof QueryStrategy) {
66 5
        $this->nameQuery = $name;
67 2
      } else {
68 2
        throw new \InvalidArgumentException('Expect string or QueryInterface');
69 2
      }
70
71 10
      return $this;
72
    }
73
74
75
    /**
76
     * @return $this
77
     */
78 3
    public function outputBody() {
79 3
      $this->outputType = self::OUTPUT_BODY;
80 3
      return $this;
81
    }
82
83
84
    /**
85
     * @return $this
86
     */
87 6
    public function outputFull() {
88 6
      $this->outputType = self::OUTPUT_FULL;
89 6
      return $this;
90
    }
91
92
93
    /**
94
     * @codeCoverageIgnore
95
     * @deprecated
96
     * @param QueryStrategy $strategy
97
     * @return $this
98
     */
99
    public function whereName(QueryStrategy $strategy) {
100
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
101
      return $this->withName($strategy);
102
    }
103
104
105
    /**
106
     * @inheritdoc
107
     */
108 28
    public function __invoke(QuerySequence $querySequence) {
109
110
111 26
      $modifier = $querySequence->process(Possible::create()->valueIs([
112 26
        'abstract',
113 13
        'final',
114 13
      ]));
115
116 26
      $querySequence->possible(T_WHITESPACE);
117 26
      $start = $querySequence->strict('class');
118 26
      $querySequence->strict(T_WHITESPACE);
119 26
      $querySequence->process($this->nameQuery);
120 26
      $startClassBody = $querySequence->search('{');
121 26
      $querySequence->moveToToken($startClassBody);
122 26
      $body = $querySequence->section('{', '}');
123
124 26
      if ($modifier->isValid()) {
125 4
        $start = $modifier;
126 2
      }
127
128 26
      if (!$querySequence->isValid()) {
129 27
        return null;
130
      }
131
132
133 26
      if ($this->outputType == self::OUTPUT_BODY) {
134 20
        return $body->extractItems(1, -1);
135
      }
136
137
      # self::OUTPUT_FULL
138 4
      return $querySequence->getCollection()->extractByTokens($start, $body->getLast());
139
    }
140
141
  }