Search::setDirection()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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