LineEndingAbstract::getFindQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2.0078
1
<?php
2
3
  namespace Funivan\Cs\Tools\LineEnding;
4
5
  use Funivan\Cs\FileTool\FileTool;
6
  use Funivan\Cs\Fs\File;
7
  use Funivan\Cs\Fs\FileFilter;
8
  use Funivan\PhpTokenizer\Query\Query;
9
  use Funivan\PhpTokenizer\Token;
10
11
  /**
12
   * @author Ivan Shcherbak <[email protected]> 2016
13
   */
14
  abstract class LineEndingAbstract implements FileTool {
15
16
    const REGEX = '!\r\n?!';
17
18
    /**
19
     * @var Query
20
     */
21
    private $query;
22
23
24
    /**
25
     * @codeCoverageIgnore
26
     * @param File $file
27
     * @return boolean
28
     */
29
    public function canProcess(File $file) {
30
      return (new FileFilter())->mimeType(['/text/'])->notDeleted()->isValid($file);
31
    }
32
33
34
    /**
35
     * @codeCoverageIgnore
36
     * @return string
37
     */
38
    public function getDescription() {
39
      return 'Use correct line ending';
40
    }
41
42
43
    /**
44
     * @return Query
45
     */
46 8
    protected function getFindQuery() {
47 8
      if ($this->query !== null) {
48
        return $this->query;
49
      }
50
51 8
      $this->query = new Query();
52 8
      $this->query->custom(function (Token $token) {
53 8
        return (boolean) preg_match(LineEndingAbstract::REGEX, $token->getValue());
54 8
      });
55
56 8
      return $this->query;
57
    }
58
59
60
    /**
61
     * @param File $file
62
     * @return \Funivan\PhpTokenizer\Collection
63
     */
64
    protected function getInvalidStartTokens(File $file) {
65
      $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
66
      return $collection->find($this->getFindQuery());
67
    }
68
69
  }