Completed
Push — master ( 490a20...238f00 )
by Shcherbak
03:04
created

LineEndingAbstract::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 3
rs 10
c 2
b 0
f 1
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
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
52
    /**
53
     * @return Query
54
     */
55 8
    protected function getFindQuery() {
56 8
      if ($this->query !== null) {
57
        return $this->query;
58
      }
59
60 8
      $this->query = new Query();
61 8
      $this->query->custom(function (Token $token) {
62 8
        return (boolean) preg_match(LineEndingAbstract::REGEX, $token->getValue());
63 8
      });
64
65 8
      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
  }