Completed
Push — master ( ab7dfd...201082 )
by Shcherbak
02:27
created

Search::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 3
Bugs 2 Features 2
Metric Value
c 3
b 2
f 2
dl 0
loc 28
ccs 14
cts 15
cp 0.9333
rs 8.439
cc 5
eloc 16
nc 5
nop 2
crap 5.0073
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Strategy;
4
5
  use Funivan\PhpTokenizer\Exception\InvalidArgumentException;
6
7
  /**
8
   *
9
   * @package Funivan\PhpTokenizer\Query\Strategy
10
   */
11
  class Search extends QueryStrategy {
12
13
    /**
14
     * Move forward flag
15
     *
16
     * @var int
17
     */
18
    const FORWARD = 1;
19
20
    /**
21
     * Move backward flag
22
     *
23
     * @var int
24
     */
25
    const BACKWARD = -1;
26
27
    /**
28
     * @var int
29
     */
30
    protected $direction = 1;
31
32
33
    /**
34
     * @inheritdoc
35
     */
36 69
    public function process(\Funivan\PhpTokenizer\Collection $collection, $currentIndex) {
37
38 69
      $result = new StrategyResult();
39
40
      # getProcessor while we can check toke
41
42 69
      $index = $currentIndex;
43 69
      $searchForward = ($this->direction === static::FORWARD);
44
45
      do {
46
47 69
        $token = $collection->offsetGet($index);
48 69
        if ($token === null) {
49
          return $result;
50
        }
51 69
        $index = $searchForward ? ++$index : --$index;
52
53 69
        if ($this->isValid($token)) {
54 69
          $result->setNexTokenIndex($index);
55 69
          $result->setValid(true);
56 69
          $result->setToken($token);
57 69
          break;
58
        }
59
60 63
      } while (!empty($token));
61
62 69
      return $result;
63
    }
64
65
66
    /**
67
     * @param int $direction
68
     * @return $this
69
     */
70 6
    public function setDirection($direction) {
71
72 6
      if ($direction !== static::FORWARD and $direction !== static::BACKWARD) {
73 3
        throw new InvalidArgumentException('Invalid direction option');
74
      }
75
76 3
      $this->direction = $direction;
77 3
      return $this;
78
    }
79
80
  }