Completed
Push — master ( e38aa3...d84f06 )
by Shcherbak
07:25
created

MethodPattern   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 53
ccs 20
cts 21
cp 0.9524
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A withName() 0 11 3
A __invoke() 0 14 2
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Pattern\Patterns;
4
5
  use Funivan\PhpTokenizer\Collection;
6
  use Funivan\PhpTokenizer\QuerySequence\QuerySequence;
7
  use Funivan\PhpTokenizer\Strategy\QueryStrategy;
8
  use Funivan\PhpTokenizer\Strategy\Strict;
9
10
  /**
11
   * Find method in code
12
   */
13
  class MethodPattern implements PatternInterface {
14
15
    /**
16
     * @var QueryStrategy
17
     */
18
    private $nameQuery;
19
20
21
    /**
22
     * MethodPattern constructor.
23
     */
24 8
    public function __construct() {
25 6
      $this->nameQuery = Strict::create()->valueLike('!.+!');
26 8
    }
27
28
29
    /**
30
     * @param string|QueryStrategy $name
31
     * @return $this
32
     */
33 6
    public function withName($name) {
34 4
      if (is_string($name)) {
35 2
        $this->nameQuery = Strict::create()->valueIs($name);
36 4
      } elseif ($name instanceof QueryStrategy) {
37 3
        $this->nameQuery = $name;
38 1
      } else {
39 2
        throw new \InvalidArgumentException('Invalid name format. Expect string or Query');
40
      }
41
42 2
      return $this;
43
    }
44
45
46
    /**
47
     * @param QuerySequence $querySequence
48
     * @return Collection|null
49
     */
50 4
    public function __invoke(QuerySequence $querySequence) {
51
52 4
      $querySequence->strict('function');
53 4
      $querySequence->strict(T_WHITESPACE);
54 4
      $querySequence->process($this->nameQuery);
55 4
      $querySequence->section('(', ')');
56 4
      $body = $querySequence->section('{', '}');
57
58 4
      if ($querySequence->isValid()) {
59 4
        return $body->extractItems(1, -1);
60
      }
61
62 4
      return null;
63
    }
64
65
  }