Completed
Push — master ( 7d7c0f...5ee576 )
by Shcherbak
02:02
created

ClassPattern::withoutDocComment()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 1
nop 0
crap 2
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
  use Funivan\PhpTokenizer\Token;
10
11
  /**
12
   * Pattern used to finding classes in tour source code
13
   *
14
   */
15
  class ClassPattern implements PatternInterface {
16
17
    /**
18
     * Result of this pattern will be body of the class
19
     */
20
    const OUTPUT_BODY = 1;
21
22
    /**
23
     * Result of this pattern will be full class
24
     */
25
    const OUTPUT_FULL = 2;
26
27
28
    /**
29
     * @var QueryStrategy
30
     */
31
    private $nameQuery = null;
32
33
    /**
34
     * @var callable
35
     */
36
    private $docCommentQuery;
37
38
    /**
39
     * @var int
40
     */
41
    private $outputType = self::OUTPUT_BODY;
42
43
44
    /**
45
     * By default we search for classes with any name
46
     */
47 50
    public function __construct() {
48 50
      $this->nameQuery = Strict::create()->valueLike('!.+!');
49 50
      $this->withPossibleDocComment();
50 50
    }
51
52
53
    /**
54
     * @codeCoverageIgnore
55
     * @deprecated
56
     * @param string $name
57
     * @return $this
58
     */
59
    public function nameIs($name) {
60
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
61
      return $this->withName($name);
62
    }
63
64
65
    /**
66
     * @param QueryStrategy|string $name
67
     * @return $this
68
     */
69 14
    public function withName($name) {
70 14
      if (is_string($name)) {
71 8
        $this->nameQuery = Strict::create()->valueIs($name);
72 14
      } elseif ($name instanceof QueryStrategy) {
73 6
        $this->nameQuery = $name;
74 6
      } else {
75 3
        throw new \InvalidArgumentException('Expect string or QueryInterface');
76
      }
77
78 11
      return $this;
79
    }
80
81
82
    /**
83
     * @return $this
84
     */
85 3
    public function withDocComment() {
86
      $this->docCommentQuery = function (Token $comment, QuerySequence $q) {
87 3
        if ($comment->getType() != T_DOC_COMMENT) {
88 3
          $q->setValid(false);
89 3
        }
90 3
      };
91 3
      return $this;
92
    }
93
94
95
    /**
96
     * @return $this
97
     */
98 50
    public function withPossibleDocComment() {
99
      $this->docCommentQuery = function (Token $comment, QuerySequence $q) {
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $q is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100 41
        return;
101
      };
102 50
      return $this;
103
    }
104
105
106
    /**
107
     * @return $this
108
     */
109
    public function withoutDocComment() {
110 3
      $this->docCommentQuery = function (Token $comment, QuerySequence $q) {
111 3
        if ($comment->getType() == T_DOC_COMMENT) {
112 3
          $q->setValid(false);
113 3
        }
114 3
      };
115 3
      return $this;
116
    }
117
118
119
    /**
120
     * @return $this
121
     */
122 3
    public function outputBody() {
123 3
      $this->outputType = self::OUTPUT_BODY;
124 3
      return $this;
125
    }
126
127
128
    /**
129
     * @return $this
130
     */
131 15
    public function outputFull() {
132 15
      $this->outputType = self::OUTPUT_FULL;
133 15
      return $this;
134
    }
135
136
137
    /**
138
     * @codeCoverageIgnore
139
     * @deprecated
140
     * @param QueryStrategy $strategy
141
     * @return $this
142
     */
143
    public function whereName(QueryStrategy $strategy) {
144
      trigger_error("Deprecated. Use withName", E_USER_DEPRECATED);
145
      return $this->withName($strategy);
146
    }
147
148
149
    /**
150
     * @inheritdoc
151
     */
152 47
    public function __invoke(QuerySequence $querySequence) {
153
154
155 47
      $comment = $querySequence->process(Possible::create()->typeIs(T_DOC_COMMENT));
156
157 47
      $querySequence->possible(T_WHITESPACE);
158 47
      $modifier = $querySequence->process(Possible::create()->valueIs([
159 47
        'abstract',
160 47
        'final',
161 47
      ]));
162
163 47
      $querySequence->possible(T_WHITESPACE);
164 47
      $start = $querySequence->strict('class');
165 47
      $querySequence->strict(T_WHITESPACE);
166 47
      $querySequence->process($this->nameQuery);
167 47
      $startClassBody = $querySequence->search('{');
168 47
      $querySequence->moveToToken($startClassBody);
169 47
      $body = $querySequence->section('{', '}');
170
171 47
      if ($modifier->isValid()) {
172 44
        $start = $modifier;
173 15
      }
174
175 47
      if ($comment->isValid()) {
176 9
        $start = $comment;
177 9
      }
178
179 47
      $docCommentChecker = $this->docCommentQuery;
180 47
      $docCommentChecker($comment, $querySequence);
181
182 47
      if (!$querySequence->isValid()) {
183 47
        return null;
184
      }
185
186
187 44
      if ($this->outputType == self::OUTPUT_BODY) {
188 29
        return $body->extractItems(1, -1);
189
      }
190
191
      # self::OUTPUT_FULL
192 15
      return $querySequence->getCollection()->extractByTokens($start, $body->getLast());
193
    }
194
195
  }