Completed
Push — master ( ef36c3...5844c1 )
by Shcherbak
02:04
created

FunctionCallPattern::__invoke()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 37
ccs 3
cts 3
cp 1
rs 4.909
cc 9
eloc 21
nc 14
nop 1
crap 9
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Pattern\Patterns;
4
5
  use Funivan\PhpTokenizer\Pattern\PatternMatcher;
6
  use Funivan\PhpTokenizer\Query\Query;
7
  use Funivan\PhpTokenizer\QuerySequence\QuerySequence;
8
9
  /**
10
   *
11
   */
12
  class FunctionCallPattern implements PatternInterface {
13
14
    const OUTPUT_FULL = 1;
15
16
    const OUTPUT_ARGUMENTS = 2;
17
18
    /**
19
     * @var Query|null
20
     */
21
    private $nameQuery;
22
23
    /**
24
     * @var ArgumentsPattern
25
     */
26
    private $parametersPattern;
27
28
    /**
29 3
     * @var ArgumentsPattern
30 3
     */
31 3
    private $outputType = self::OUTPUT_FULL;
32
33
34
    /**
35
     * @return $this
36
     */
37
    public function outputFull() {
38
      $this->outputType = self::OUTPUT_FULL;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::OUTPUT_FULL of type integer is incompatible with the declared type object<Funivan\PhpTokeni...terns\ArgumentsPattern> of property $outputType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39 3
      return $this;
40 3
    }
41 3
42
43
    /**
44
     * @return $this
45
     */
46
    public function outputArguments() {
47
      $this->outputType = self::OUTPUT_ARGUMENTS;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::OUTPUT_ARGUMENTS of type integer is incompatible with the declared type object<Funivan\PhpTokeni...terns\ArgumentsPattern> of property $outputType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48 9
      return $this;
49
    }
50 9
51 9
52 3
    /**
53
     * @param Query $query
54
     * @return $this
55 9
     */
56 9
    public function withName(Query $query) {
57
      $this->nameQuery = $query;
58 9
      return $this;
59 9
    }
60
61
62 9
    /**
63 9
     * @param ArgumentsPattern $pattern
64 9
     * @return $this
65 9
     */
66 6
    public function withParameters(ArgumentsPattern $pattern) {
67
      $this->parametersPattern = $pattern;
68 9
      return $this;
69 3
    }
70
71
72 9
    /**
73 3
     * @inheritdoc
74 3
     */
75 3
    public function __invoke(QuerySequence $querySequence) {
76
77 2
      $name = $querySequence->strict(T_STRING);
78
      if ($this->nameQuery !== null and $this->nameQuery->isValid($name) === false) {
79 9
        return null;
80
      }
81
82
      $querySequence->possible(T_WHITESPACE);
83
      $arguments = $querySequence->section('(', ')');
84
85
      if (!$querySequence->isValid()) {
86
        return null;
87
      }
88
89
      $querySequence->moveToToken($name);
90
      $before = $querySequence->move(-1);
91
      if ($before->getType() === T_WHITESPACE) {
92
        $before = $querySequence->move(-1);
93
      }
94
95
      if (in_array($before->getValue(), ['::', 'function', '->'])) {
96
        return null;
97
      }
98
99
      if ($this->parametersPattern !== null) {
100
        $pattern = (new PatternMatcher($arguments))->apply($this->parametersPattern);
101
        if (count($pattern->getCollections()) === 0) {
102
          return null;
103
        }
104
      }
105
106
      if ($this->outputType === self::OUTPUT_ARGUMENTS) {
107
        return $arguments;
108
      }
109
110
      return $querySequence->getCollection()->extractByTokens($name, $arguments->getLast());
111
    }
112
113
  }