UseSortingManager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.88%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 91
ccs 31
cts 32
cp 0.9688
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sort() 0 14 2
A findUseStatements() 0 21 3
A createCleanedFile() 0 16 2
1
<?php
2
3
namespace Matks\PHPMakeUp\UseSorting;
4
5
use Exception;
6
use Matks\PHPMakeUp\File\FileManagerInterface;
7
8
/**
9
 * Use Sorting Manager
10
 */
11
class UseSortingManager implements UseSortingManagerInterface
12
{
13
    /**
14
     * Use statement regex
15
     */
16
    const USE_STATEMENT_REGEX = '^(use .*)$';
17
18
    /**
19
     * Constructor
20
     *
21
     * @param FileManagerInterface $fileManager
22
     */
23
    public function __construct(FileManagerInterface $fileManager)
24
    {
25 1
        $this->fileManager = $fileManager;
0 ignored issues
show
Bug introduced by
The property fileManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26 1
    }
27
28
    /**
29
     * Search and sort alphabetically use statements in a class file
30
     *
31
     * @param string $filepath
32
     *
33
     * @throws Exception
34
     */
35
    public function sort($filepath)
36 1
    {
37 1
        if (!file_exists($filepath)) {
38
            throw new Exception("File $filepath does not exist");
39
        }
40
41 1
        $statementBlock = $this->findUseStatements($filepath);
42
43 1
        $fileLines = $this->createCleanedFile($filepath, $statementBlock);
44
45 1
        $newFilepath = $filepath . '.copy';
46 1
        $this->fileManager->writeFile($newFilepath, $fileLines);
47 1
        $this->fileManager->replaceFile($filepath, $newFilepath);
48 1
    }
49
50
    /**
51
     * Find use statements
52
     *
53
     * @param string $filepath
54
     *
55
     * @return UseStatementBlock
56
     */
57
    private function findUseStatements($filepath)
58
    {
59 1
        $lines = file($filepath);
60
61 1
        $statementBlock = new UseStatementBlock();
62
63 1
        foreach ($lines as $lineNumber => $line) {
64
65 1
            $matches = array();
66 1
            $result  = preg_match('#' . static::USE_STATEMENT_REGEX . '#', $line, $matches);
67
68 1
            if (0 === $result) {
69 1
                continue;
70
            }
71
72 1
            $statementContent = $matches[1];
73 1
            $statementBlock->addStatement($lineNumber, $statementContent);
74 1
        }
75
76 1
        return $statementBlock;
77
    }
78
79
    /**
80
     * Sort use lines in new created file
81
     *
82
     * @param string            $filepath
83
     * @param UseStatementBlock $statementBlock
84
     */
85
    private function createCleanedFile($filepath, UseStatementBlock $statementBlock)
86
    {
87 1
        $fileLines        = file($filepath);
88 1
        $sortedStatements = $statementBlock->getSortedStatements();
89 1
        $lineNumbers      = $statementBlock->getSortedLineNumbers();
90
91 1
        $i = 0;
92 1
        foreach ($sortedStatements as $statement) {
93 1
            $lineNumber             = $lineNumbers[$i];
94 1
            $fileLines[$lineNumber] = $statement . PHP_EOL;
95
96 1
            $i++;
97 1
        }
98
99 1
        return $fileLines;
100
    }
101
}
102