Skip to content

Commit 2c134df

Browse files
author
Igor Hrcek
authored
Merge pull request #42 from igorhrcek/issue-37
Issue 37
2 parents 3d9ccc7 + a431781 commit 2c134df

26 files changed

Lines changed: 352 additions & 254 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ vendor/
66
nginx.conf
77
.DS_Store
88
wp/
9+
composer.lock
10+
wp_cli_secure.iml

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"require-dev": {
2828
"guzzlehttp/guzzle": "^7.0",
2929
"mikey179/vfsstream": "^1.6",
30+
"phpstan/phpstan": "^1.5",
3031
"phpunit/phpunit": "^9.5",
32+
"squizlabs/php_codesniffer": "^3.6",
3133
"vlucas/phpdotenv": "^5.4",
3234
"wp-cli/wp-cli-bundle": "*",
3335
"wp-cli/wp-cli-tests": "^3.1"

src/Exceptions/FileDoesNotExist.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Exception;
66

7-
class FileDoesNotExist extends Exception {
7+
class FileDoesNotExist extends Exception
8+
{
89
/**
910
* @var string
1011
*/
1112
protected $message = 'File does not exist';
12-
}
13+
}

src/Exceptions/FileIsNotReadable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Exception;
66

7-
class FileIsNotReadable extends Exception {
7+
class FileIsNotReadable extends Exception
8+
{
89
/**
910
* @var string
1011
*/
1112
protected $message = 'File does not have a correct permissions to read its content';
12-
}
13+
}

src/Exceptions/FileIsNotWritable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Exception;
66

7-
class FileIsNotWritable extends Exception {
7+
class FileIsNotWritable extends Exception
8+
{
89
/**
910
* @var string
1011
*/
1112
protected $message = 'File does not have a correct permissions for writing';
12-
}
13+
}

src/Exceptions/RuleAlreadyExist.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Exception;
66

7-
class RuleAlreadyExist extends Exception {
7+
class RuleAlreadyExist extends Exception
8+
{
89
/**
910
* @var string
1011
*/
1112
protected $message = 'The rule already exists in the file';
12-
}
13+
}

src/FileManager.php

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use WP_CLI_Secure\Exceptions\FileIsNotWritable;
1010
use WP_CLI_Secure\Exceptions\RuleAlreadyExist;
1111

12-
class FileManager {
12+
class FileManager
13+
{
1314
/**
1415
* Defines the end of each marked block
1516
* # END WP CLI SECURE
@@ -65,7 +66,8 @@ class FileManager {
6566
*/
6667
private array $file;
6768

68-
public function __construct(string $path) {
69+
public function __construct(string $path)
70+
{
6971
$this->path = $path;
7072
$this->file = $this->setFileContent();
7173
}
@@ -76,12 +78,13 @@ public function __construct(string $path) {
7678
* @return array
7779
* @throws FileIsNotReadable
7880
*/
79-
private function setFileContent() : array {
80-
if(!$this->fileExist()) {
81+
private function setFileContent(): array
82+
{
83+
if (!$this->fileExist()) {
8184
return [];
8285
}
8386

84-
if(!$this->isReadable()) {
87+
if (!$this->isReadable()) {
8588
throw new FileIsNotReadable();
8689
}
8790

@@ -96,18 +99,19 @@ private function setFileContent() : array {
9699
*
97100
* @return array
98101
*/
99-
public function read(int $startLine = 0, int $lines = null) : array {
102+
public function read(int $startLine = 0, int $lines = null): array
103+
{
100104
$result = [];
101105
$file = new \SplFileObject($this->path);
102106
$file->seek($startLine);
103107

104-
if($lines === null) {
105-
while(!$file->eof()) {
108+
if ($lines === null) {
109+
while (!$file->eof()) {
106110
$result[] = rtrim($file->current(), "\n");
107111
$file->next();
108112
}
109113
} else {
110-
for($i = 0; $i < $lines; $i++) {
114+
for ($i = 0; $i < $lines; $i++) {
111115
if ($file->eof()) {
112116
break;
113117
}
@@ -127,7 +131,8 @@ public function read(int $startLine = 0, int $lines = null) : array {
127131
*
128132
* @return bool
129133
*/
130-
private function isWritable() : bool {
134+
private function isWritable(): bool
135+
{
131136
return is_writable($this->path);
132137
}
133138

@@ -136,7 +141,8 @@ private function isWritable() : bool {
136141
*
137142
* @return bool
138143
*/
139-
private function isReadable(): bool {
144+
private function isReadable(): bool
145+
{
140146
return is_readable($this->path);
141147
}
142148

@@ -145,20 +151,21 @@ private function isReadable(): bool {
145151
*
146152
* @return bool
147153
*/
148-
private function fileExist() : bool {
154+
private function fileExist(): bool
155+
{
149156
return file_exists($this->path);
150157
}
151158

152159
/**
153160
* Remove ghost Unicode spaces and other unnecessary stuff (<200b><200c>)
154161
*
155-
* @param $content
162+
* @param string|array $content
156163
*
157164
* @return array|string
158165
*/
159166
private static function removeZeroSpace($content)
160167
{
161-
if(is_array($content)) {
168+
if (is_array($content)) {
162169
return array_map([static::class, 'removeZeroSpace'], $content);
163170
}
164171

@@ -177,20 +184,21 @@ private static function removeZeroSpace($content)
177184
*
178185
* @return array
179186
*/
180-
public function extractRuleBlock(string $marker) : array {
187+
public function extractRuleBlock(string $marker): array
188+
{
181189
$result = [];
182190

183191
$found = false;
184-
foreach($this->file as $line) {
185-
if(strpos($line, $marker . self::SPACE_DELIMITER . self::MARKER_RULE_END) !== false) {
192+
foreach ($this->file as $line) {
193+
if (strpos($line, $marker . self::SPACE_DELIMITER . self::MARKER_RULE_END) !== false) {
186194
$found = false;
187195
}
188196

189-
if($found) {
197+
if ($found) {
190198
$result[] = $line;
191199
}
192200

193-
if(strpos($line, $marker . self::SPACE_DELIMITER . self::MARKER_RULE_START) !== false) {
201+
if (strpos($line, $marker . self::SPACE_DELIMITER . self::MARKER_RULE_START) !== false) {
194202
$found = true;
195203
}
196204
}
@@ -205,7 +213,8 @@ public function extractRuleBlock(string $marker) : array {
205213
*
206214
* @return bool|int|string
207215
*/
208-
private function findInFile(string $needle) {
216+
private function findInFile(string $needle)
217+
{
209218
return array_search($needle, $this->file);
210219
}
211220

@@ -214,22 +223,23 @@ private function findInFile(string $needle) {
214223
*
215224
* @return array|bool
216225
*/
217-
public function extractSecureBlock() {
226+
public function extractSecureBlock()
227+
{
218228
$start = $this->findInFile(self::MARKER_GLOBAL_START . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
219229
$end = $this->findInFile(self::MARKER_GLOBAL_END . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
220230

221-
if($start === false || $end === false) {
231+
if ($start === false || $end === false) {
222232
return false;
223233
}
224234

225235
$result = array_slice($this->file, $start + 1, $end - $start - 1);
226236

227-
if(!$result) {
237+
if (!$result) {
228238
return false;
229239
}
230240

231-
if(count($result) === 1) {
232-
return trim($result[0]);
241+
if (count($result) === 1) {
242+
return [trim($result[0])];
233243
}
234244

235245
return array_filter($result);
@@ -242,7 +252,8 @@ public function extractSecureBlock() {
242252
*
243253
* @return bool
244254
*/
245-
public function hasRuleBlock(string $marker) : bool {
255+
public function hasRuleBlock(string $marker): bool
256+
{
246257
return count($this->extractRuleBlock($marker)) > 0;
247258
}
248259

@@ -251,7 +262,8 @@ public function hasRuleBlock(string $marker) : bool {
251262
*
252263
* @return bool
253264
*/
254-
public function hasSecureBlock() : bool {
265+
public function hasSecureBlock(): bool
266+
{
255267
return is_array($this->extractSecureBlock()) && count($this->extractSecureBlock()) > 0;
256268
}
257269

@@ -262,15 +274,16 @@ public function hasSecureBlock() : bool {
262274
*
263275
* @return bool
264276
*/
265-
public function createFile(int $permissions = 0644) : bool {
266-
if(!$this->fileExist()) {
277+
public function createFile(int $permissions = 0644): bool
278+
{
279+
if (!$this->fileExist()) {
267280
$path = explode("/", $this->path);
268281
$fileName = $path[count($path) - 1];
269282
$basePath = implode('/', array_slice($path, 0, count($path) - 1));
270283
$pathInfo = pathinfo($this->path);
271284

272285
//Create a full directory path if it does not exist
273-
if(!file_exists($pathInfo['dirname'])) {
286+
if (!file_exists($pathInfo['dirname'])) {
274287
mkdir($pathInfo['dirname'], 0755, true);
275288
}
276289

@@ -293,11 +306,12 @@ public function createFile(int $permissions = 0644) : bool {
293306
*
294307
* @return array
295308
*/
296-
public function wrap(array $content, string $with = 'block', string $marker = '') : array {
297-
if($with === 'block') {
309+
public function wrap(array $content, string $with = 'block', string $marker = ''): array
310+
{
311+
if ($with === 'block') {
298312
array_unshift($content, self::MARKER_RULE . self::SPACE_DELIMITER . $marker . self::SPACE_DELIMITER . self::MARKER_RULE_START);
299313
array_push($content, self::MARKER_RULE . self::SPACE_DELIMITER . $marker . self::SPACE_DELIMITER . self::MARKER_RULE_END);
300-
} else if($with === 'global') {
314+
} elseif ($with === 'global') {
301315
array_unshift($content, self::MARKER_GLOBAL_START . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
302316
array_push($content, self::MARKER_GLOBAL_END . self::SPACE_DELIMITER . self::MARKER_WP_CLI_SECURE);
303317
}
@@ -310,9 +324,9 @@ public function wrap(array $content, string $with = 'block', string $marker = ''
310324
*
311325
* @return bool
312326
*/
313-
private function backup() : bool
327+
private function backup(): bool
314328
{
315-
if(file_exists($this->path . self::BACKUP_EXTENSION)) {
329+
if (file_exists($this->path . self::BACKUP_EXTENSION)) {
316330
unlink($this->path . self::BACKUP_EXTENSION);
317331
}
318332

@@ -327,25 +341,26 @@ private function backup() : bool
327341
*
328342
* @return bool
329343
*/
330-
public function add(array $content, string $marker = ''): bool {
344+
public function add(array $content, string $marker = ''): bool
345+
{
331346
//If the rule block already exist, there is no reason to add it again
332-
if($this->hasRuleBlock($marker)) {
347+
if ($this->hasRuleBlock($marker)) {
333348
throw new RuleAlreadyExist();
334349
}
335350

336351
//Check if file exist?
337-
if(!$this->fileExist()) {
352+
if (!$this->fileExist()) {
338353
$this->createFile();
339354
}
340355

341-
if(!$this->isWritable()) {
356+
if (!$this->isWritable()) {
342357
throw new FileIsNotWritable();
343358
}
344359

345360
//Wrap the rule block with markers
346361
$content = $this->wrap($content, 'block', $marker);
347362

348-
if(!$this->hasSecureBlock()) {
363+
if (!$this->hasSecureBlock()) {
349364
$content = $this->wrap($content, 'global');
350365

351366
//File does not contain any of our SECURE rules, so we need to prepend our content at the beginning of the file
@@ -365,8 +380,9 @@ public function add(array $content, string $marker = ''): bool {
365380
*
366381
* @return bool
367382
*/
368-
public function clear() : bool {
369-
if(!$this->hasSecureBlock()) {
383+
public function clear(): bool
384+
{
385+
if (!$this->hasSecureBlock()) {
370386
return true;
371387
}
372388

@@ -385,8 +401,9 @@ public function clear() : bool {
385401
*
386402
* @return bool
387403
*/
388-
public function remove(string $marker) : bool {
389-
if(!$this->hasRuleBlock($marker)) {
404+
public function remove(string $marker): bool
405+
{
406+
if (!$this->hasRuleBlock($marker)) {
390407
return true;
391408
}
392409

@@ -403,7 +420,8 @@ public function remove(string $marker) : bool {
403420
*
404421
* @return bool
405422
*/
406-
public function write(): bool {
423+
public function write(): bool
424+
{
407425
//Flatten the array (<= PHP 8.0 compatible)
408426
$this->file = $this->flattenArray($this->file);
409427

@@ -419,7 +437,7 @@ public function write(): bool {
419437
fseek($fp, 0);
420438
$write = fwrite($fp, $content);
421439

422-
if($write) {
440+
if ($write) {
423441
ftruncate($fp, ftell($fp));
424442
}
425443
fflush($fp);
@@ -438,7 +456,8 @@ public function write(): bool {
438456
*
439457
* @return array
440458
*/
441-
private function flattenArray(array $array, int $depth = 1) : array {
459+
private function flattenArray(array $array, int $depth = 1): array
460+
{
442461
$result = [];
443462
foreach ($array as $key => $value) {
444463
if (is_array($value) && $depth) {

0 commit comments

Comments
 (0)