Completed
Push — master ( 2855be...490a20 )
by Shcherbak
02:34
created

LineEndingAbstract::getFindQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
3
  namespace Funivan\Cs\Tools\LineEnding;
4
5
  use Funivan\Cs\FileFinder\FileInfo;
6
  use Funivan\Cs\FileProcessor\CanProcessHelper;
7
  use Funivan\Cs\FileProcessor\FileTool;
8
  use Funivan\PhpTokenizer\Query\Query;
9
  use Funivan\PhpTokenizer\Token;
10
11
  /**
12
   * @todo add configuration. User specify
13
   */
14
  abstract class LineEndingAbstract implements FileTool {
15
16
    /**
17
     * @var array
18
     */
19
    private static $lineEndings = [
0 ignored issues
show
Unused Code introduced by
The property $lineEndings is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
      'crlf' => "\r\n",
21
      'lf' => "\n",
22
      'cr' => "\r",
23
    ];
24
25
    const REGEX = '!\r\n?!';
26
27
    /**
28
     * @var Query
29
     */
30
    private $query;
31
32
33
    /**
34
     * @codeCoverageIgnore
35
     * @param FileInfo $file
36
     * @return boolean
37
     */
38
    public function canProcess(FileInfo $file) {
39
      return (new CanProcessHelper())->mimeType('text')->notDeleted()->isValid($file);
40
    }
41
42
43
    /**
44
     * @codeCoverageIgnore
45
     * @return string
46
     */
47
    public function getDescription() {
48
      return 'Use correct line ending';
49
    }
50
51 8
52
    /**
53 8
     * @return Query
54 8
     */
55 8
    protected function getFindQuery() {
56 8
      if ($this->query !== null) {
57 8
        return $this->query;
58
      }
59 8
60
      $this->query = new Query();
61
      $this->query->custom(function (Token $token) {
62
        return (boolean) preg_match(LineEndingAbstract::REGEX, $token->getValue());
63
      });
64
65
      return $this->query;
66
    }
67
68
69
    /**
70
     * @param FileInfo $file
71
     * @return \Funivan\PhpTokenizer\Collection
72
     */
73
    protected function getInvalidStartTokens(FileInfo $file) {
74
      $collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get());
75
      return $collection->find($this->getFindQuery());
76
    }
77
78
  }