Completed
Push — next ( a537c2...29cbfe )
by Thomas
08:43
created

StyleCleanUp::getFilesModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 ***************************************************************************/
7
8
namespace Oc\Util;
9
10
class StyleCleanUp
11
{
12
    private $tabWith = 4;
13
14
    /**
15
     * @var array
16
     */
17
    private $excludeDirs;
18
19
    /**
20
     * @var string
21
     */
22
    private $basedir;
23
24
    /**
25
     * @var int
26
     */
27
    private $filesModified;
28
29
    /**
30
     * @var int
31
     */
32
    private $linesModified;
33
34
    /**
35
     * @param string $basedir
36
     * @param array $excludeDirs
37
     */
38
    public function run($basedir, $excludeDirs)
39
    {
40
        $this->basedir = $basedir;
41
        $this->excludeDirs = $excludeDirs;
42
        $this->filesModified = 0;
43
        $this->linesModified = 0;
44
45
        $this->cleanup($basedir);
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getFilesModified()
52
    {
53
        return $this->filesModified;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getLinesModified()
60
    {
61
        return $this->linesModified;
62
    }
63
64
    /**
65
     * @param $path
66
     */
67
    private function cleanup($path)
68
    {
69
        if (!in_array(substr($path, strlen($this->basedir) + 1), $this->excludeDirs, true)) {
70
            # process files in $path
71
72
            $files = array_merge(
73
                glob($path . '/*.php'),
74
                glob($path . '/*.tpl')
75
            );
76
77
            foreach ($files as $filePath) {
78
                $file_modified = false;
79
                $lines = file($filePath);
80
                $displayFilePath = substr($filePath, strlen($this->basedir) + 1);
81
82
                # detect illegal characters at start of PHP or XML file
83
84
                if (count($lines) && preg_match('/^(.+?)\<\?/', $lines[0], $matches)) {
85
                    $this->warn(
86
                        'invalid character(s) "' . $matches[1] . '" at start of ' . $displayFilePath
87
                    );
88
                }
89
90
                # Remove trailing whitespaces, strip CRs, expand tabs, make
91
                # sure that all - including the last - line end on "\n",
92
                # and detect short open tags. Only-whitespace lines are
93
                # allowed by PSR-2 and will not be trimmed.
94
95
                $n = 1;
96
                foreach ($lines as &$line) {
97
                    if (!preg_match("/^ *(\\*|\/\/|#) *\n$/", $line)
98
                        && (trim($line, " \n") !== '' || substr($line, -1) !== "\n")
99
                    ) {
100
                        $oldLine = $line;
101
                        $line = rtrim($line);   # trims " \t\n\r\0\x0B"
102
                        $line = $this->expandTabs($line);
103
                        $line .= "\n";
104
105
                        if ($line != $oldLine) {
106
                            $file_modified = true;
107
                            ++$this->linesModified;
108
                        }
109
                    }
110
                    if (preg_match('/\<\?\s/', $line)) {   # relies on \n at EOL
111
                        $this->warn('short open tag in line ' . $n . ' of ' . $displayFilePath);
112
                    }
113
                    ++$n;
114
                }
115
                unset($line);
116
117
                # remove PHP close tags and empty lines from end of file
118
119
                $l = count($lines) - 1;
120
                while ($l > 0) {
121
                    $trimmed_line = trim($lines[$l]);
122
                    if ($trimmed_line === '?>' || $trimmed_line === '') {
123
                        unset($lines[$l]);
124
                        $file_modified = true;
125
                        ++$this->linesModified;
126
                    } else {
127
                        break;
128
                    }
129
                    --$l;
130
                }
131
132
                if ($file_modified) {
133
                    echo 'cleaned ' . substr($filePath, 2) . "\n";
134
                    file_put_contents($filePath, implode('', $lines));
135
                    ++$this->filesModified;
136
                }
137
            }
138
139
            # process subdirectories in $path
140
141
            $dirs = glob($path . '/*', GLOB_ONLYDIR);
142
            foreach ($dirs as $dir) {
143
                if ($dir !== '.' && $dir !== '..') {
144
                    $this->cleanup($dir);
145
                }
146
            }
147
        }
148
    }
149
150
    /**
151
     * @param $line
152
     * @return string
153
     */
154
    private function expandTabs($line)
155
    {
156
        while (($tabPos = strpos($line, "\t")) !== false) {
157
            $line =
158
                substr($line, 0, $tabPos)
159
                . substr('    ', 0, $this->tabWith - ($tabPos % $this->tabWith))
160
                . substr($line, $tabPos + 1);
161
        }
162
163
        return $line;
164
    }
165
166
    /**
167
     * @param $msg
168
     */
169
    private function warn($msg)
170
    {
171
        echo '! ' . $msg . "\n";
172
    }
173
}
174