Completed
Push — master ( ee6702...22a14a )
by Shcherbak
02:10
created

ClassPattern::withName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
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 39
    public function __construct() {
42 39
      $this->nameQuery = Strict::create()->valueLike('!.+!');
43 39
    }
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 12
      if (is_string($name)) {
64 6
        $this->nameQuery = Strict::create()->valueIs($name);
65 8
      } elseif ($name instanceof QueryStrategy) {
66 6
        $this->nameQuery = $name;
67 4
      } else {
68 3
        throw new \InvalidArgumentException('Expect string or QueryInterface');
69
      }
70
71 9
      return $this;
72
    }
73
74
75
    /**
76
     * @return $this
77
     */
78
    public function outputBody() {
79
      $this->outputType = self::OUTPUT_BODY;
80
      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 36
    public function __invoke(QuerySequence $querySequence) {
109
110 36
      $start = $querySequence->strict('class');
111 36
      $querySequence->strict(T_WHITESPACE);
112 36
      $querySequence->process($this->nameQuery);
113 36
      $startClassBody = $querySequence->search('{');
114 36
      $querySequence->moveToToken($startClassBody);
115 36
      $body = $querySequence->section('{', '}');
116
117 36
      if ($start->isValid()) {
118
        # catch class modifiers
119 33
        $querySequence->moveToToken($start);
120 33
        $querySequence->move(-2);
121 33
        $modifier = $querySequence->process(Possible::create()->valueIs([
0 ignored issues
show
Documentation introduced by
array('abstract', 'final') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122 33
          'abstract',
123 22
          'final',
124 22
        ]));
125 33
        if ($modifier->isValid()) {
126 3
          $start = $modifier;
127 2
        }
128 22
      }
129
130 36
      if (!$querySequence->isValid()) {
131 36
        return null;
132
      }
133
134 33
      if ($this->outputType == self::OUTPUT_BODY) {
135 27
        return $body->extractItems(1, -1);
136
      }
137
138 6
      if ($this->outputType == self::OUTPUT_FULL) {
139 6
        return $querySequence->getCollection()->extractByTokens($start, $body->getLast());
140
      }
141
142
    }
143
144
  }