Completed
Push — master ( 1bff1a...f9631c )
by Shcherbak
02:38
created

Move   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 4 Features 1
Metric Value
wmc 5
c 7
b 4
f 1
lcom 1
cbo 2
dl 0
loc 61
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A process() 0 15 2
A __construct() 0 9 2
1
<?php
2
3
  namespace Funivan\PhpTokenizer\Strategy;
4
5
  use Funivan\PhpTokenizer\Exception\InvalidArgumentException;
6
7
  /**
8
   * Move forward or backward in collection with relative position
9
   *
10
   * Move forward 12 steps
11
   * ```
12
   * $result = (new Move(12))->process($collection, 1);
13
   * ```
14
   *
15
   * Move backward 10 steps
16
   * ```
17
   * $result = (new Move(-10))->process($collection, 1);
18
   * ```
19
   *
20
   * @package Funivan\PhpTokenizer\BlockExtractor
21
   */
22
  class Move implements StrategyInterface {
23
24
25
    /**
26
     * @var int
27
     */
28
    protected $steps = null;
29
30
    /**
31
     * @var int
32
     */
33
    protected $direction = null;
34
35
36
    /**
37
     * @param int $steps
38
     * @return static
39
     */
40 21
    public static function create($steps) {
41 21
      return new static($steps);
42
    }
43
44
45
    /**
46
     * You can pass positive and negative numbers
47
     * Positive - move forward
48
     * Negative - move backward
49
     *
50
     * @param int $steps
51
     */
52 21
    public function __construct($steps) {
53
54 21
      if (!is_int($steps)) {
55 3
        throw new InvalidArgumentException('Invalid steps. Expect integer. Given: ' . gettype($steps));
56
      }
57
58 18
      $this->steps = $steps;
59
60 18
    }
61
62
63
    /**
64
     * @inheritdoc
65
     */
66 18
    public function process(\Funivan\PhpTokenizer\Collection $collection, $currentIndex) {
67 18
      $result = new StrategyResult();
68
69 18
      $endIndex = $currentIndex + $this->steps;
70
71 18
      $result->setNexTokenIndex($endIndex);
72 18
      if (isset($collection[$endIndex])) {
73 18
        $result->setValid(true);
74 18
        $result->setToken($collection[$endIndex]);
75 12
      } else {
76 9
        $result->setValid(false);
77
      }
78
79 18
      return $result;
80
    }
81
82
  }