LineEndingAbstract   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6
ccs 7
cts 11
cp 0.6364
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canProcess() 0 3 1
A getDescription() 0 3 1
A getFindQuery() 0 12 2
A getInvalidStartTokens() 0 4 1
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
  }