NestingConsistencyFixer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 26
c 1
b 0
f 0
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fixIssue() 0 37 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Fixer\NestingConsistency;
5
6
use Pluswerk\TypoScriptAutoFixer\File;
7
use Pluswerk\TypoScriptAutoFixer\FileBuilder;
8
use Pluswerk\TypoScriptAutoFixer\Fixer\AbstractFixer;
9
use Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue;
10
use Pluswerk\TypoScriptAutoFixer\Issue\NestingConsistencyIssue;
11
12
final class NestingConsistencyFixer extends AbstractFixer
13
{
14
    /**
15
     * @var NodeCollectionBuilder
16
     */
17
    private $nodeCollectionBuilder;
18
19
    public function __construct(FileBuilder $fileBuilder = null, NodeCollectionBuilder $nodeCollectionBuilder = null)
20
    {
21
        parent::__construct($fileBuilder);
22
        $this->nodeCollectionBuilder = $nodeCollectionBuilder ?? new NodeCollectionBuilder();
23
    }
24
25
    /**
26
     * @param File          $file
27
     * @param AbstractIssue|NestingConsistencyIssue $issue
28
     *
29
     * @return File
30
     */
31
    public function fixIssue(File $file, AbstractIssue $issue): File
32
    {
33
        if ($issue->line() < $issue->secondLine()) {
0 ignored issues
show
Bug introduced by
The method secondLine() does not exist on Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue. It seems like you code against a sub-type of Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue such as Pluswerk\TypoScriptAutoF...NestingConsistencyIssue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        if ($issue->line() < $issue->/** @scrutinizer ignore-call */ secondLine()) {
Loading history...
34
            $insertLine = $issue->secondLine();
35
            $lineNumbersA = range($issue->line(), $issue->firstEndLine());
0 ignored issues
show
Bug introduced by
The method firstEndLine() does not exist on Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue. It seems like you code against a sub-type of Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue such as Pluswerk\TypoScriptAutoF...NestingConsistencyIssue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            $lineNumbersA = range($issue->line(), $issue->/** @scrutinizer ignore-call */ firstEndLine());
Loading history...
36
            $lineNumbersB = range($issue->secondLine(), $issue->secondEndLine());
0 ignored issues
show
Bug introduced by
The method secondEndLine() does not exist on Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue. It seems like you code against a sub-type of Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue such as Pluswerk\TypoScriptAutoF...NestingConsistencyIssue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            $lineNumbersB = range($issue->secondLine(), $issue->/** @scrutinizer ignore-call */ secondEndLine());
Loading history...
37
        } else {
38
            $insertLine = $issue->line();
39
            $lineNumbersA = range($issue->secondLine(), $issue->secondEndLine());
40
            $lineNumbersB = range($issue->line(), $issue->firstEndLine());
41
        }
42
43
        $linesA = [];
44
        $linesB = [];
45
46
        foreach ($lineNumbersA as $lineNumber) {
47
            $linesA[] = $file->readLine($lineNumber);
48
        }
49
50
        foreach ($lineNumbersB as $lineNumber) {
51
            $linesB[] = $file->readLine($lineNumber);
52
        }
53
54
        $collectionA = $this->nodeCollectionBuilder->buildNodeCollectionFromMultiLine($linesA);
55
        $collectionB = $this->nodeCollectionBuilder->buildNodeCollectionFromMultiLine($linesB);
56
57
        $resultCollection = $this->nodeCollectionBuilder->mergeNodeCollections($collectionA, $collectionB);
58
59
        $file->removeLines($lineNumbersB);
60
61
        $file->insertStringToFile($insertLine, (string) $resultCollection);
62
63
        $file->removeLines($lineNumbersA);
64
65
        $file = $this->fileBuilder->buildFile($file->getPathname());
66
67
        return $file;
68
    }
69
}
70