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

MethodPattern   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

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 20
cp 1
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 9
    public function __construct() {
25 9
      $this->nameQuery = Strict::create()->valueLike('!.+!');
26 9
    }
27
28
29
    /**
30
     * @param string|QueryStrategy $name
31
     * @return $this
32
     */
33 6
    public function withName($name) {
34 6
      if (is_string($name)) {
35 3
        $this->nameQuery = Strict::create()->valueIs($name);
36 4
      } elseif ($name instanceof QueryStrategy) {
37 3
        $this->nameQuery = $name;
38 2
      } else {
39 3
        throw new \InvalidArgumentException('Invalid name format. Expect string or Query');
40
      }
41
42 3
      return $this;
43
    }
44
45
46
    /**
47
     * @param QuerySequence $querySequence
48
     * @return Collection|null
49
     */
50 6
    public function __invoke(QuerySequence $querySequence) {
51
52 6
      $querySequence->strict('function');
53 6
      $querySequence->strict(T_WHITESPACE);
54 6
      $querySequence->process($this->nameQuery);
55 6
      $querySequence->section('(', ')');
56 6
      $body = $querySequence->section('{', '}');
57
58 6
      if ($querySequence->isValid()) {
59 6
        return $body->extractItems(1, -1);
60
      }
61
62 6
      return null;
63
    }
64
65
  }