TokenFinder::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Funivan\PhpTokenizer;
6
7
  use Funivan\PhpTokenizer\Query\QueryInterface;
8
9
  /**
10
   * Simple token finder
11
   * You can pass query to search tokens in collection
12
   *
13
   * For example find all echo values
14
   *
15
   * ```
16
   * $finder = new TokenFinder($collection)
17
   * $items = $finder->find((new Query())->valueIs('echo'));
18
   *
19
   * ```
20
   *
21
   * @author Ivan Shcherbak <[email protected]> 4/17/15
22
   */
23
  class TokenFinder {
24
25
    /**
26
     * @var Collection
27
     */
28
    private $collection;
29
30
31
    /**
32
     * @param Collection $collection
33
     */
34 18
    public function __construct(Collection $collection) {
35 18
      $this->collection = $collection;
36 18
    }
37
38
39
    /**
40
     * @param QueryInterface $query
41
     * @return Collection
42
     */
43 18
    public function find(QueryInterface $query): Collection {
44 18
      $result = new Collection();
45 18
      foreach ($this->collection as $token) {
46 18
        if ($query->isValid($token)) {
47 18
          $result[] = $token;
48
        }
49
      }
50 18
      return $result;
51
    }
52
53
  }