1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of expect-filesystem package. |
5
|
|
|
* |
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
namespace expect\filesystem\matcher; |
12
|
|
|
|
13
|
|
|
use expect\FailedMessage; |
14
|
|
|
use expect\matcher\ReportableMatcher; |
15
|
|
|
use SplFileInfo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Verify a file permissions |
19
|
|
|
* |
20
|
|
|
* <code> |
21
|
|
|
* $matcher = new ToBeMode(644); |
22
|
|
|
* $matcher->match('/path/to/file'); |
23
|
|
|
* </code> |
24
|
|
|
* |
25
|
|
|
* @author Noritaka Horio <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ToBeMode implements ReportableMatcher |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $actual; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $expected; |
38
|
|
|
|
39
|
|
|
private $actualPermission; |
40
|
|
|
|
41
|
|
|
public function __construct($expected) |
42
|
|
|
{ |
43
|
|
|
$this->expected = $expected; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function match($actual) |
47
|
|
|
{ |
48
|
|
|
$this->actual = $actual; |
49
|
|
|
|
50
|
|
|
$value = substr(sprintf('%o', fileperms($this->actual)), -4); //Example: 100444 |
51
|
|
|
$this->actualPermission = intval($value, 8); |
52
|
|
|
|
53
|
|
|
return $this->actualPermission === $this->expected; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function reportFailed(FailedMessage $message) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$message->appendText('Expected ') |
62
|
|
|
->appendValue($this->actual) |
63
|
|
|
->appendText(' to be ') |
64
|
|
|
->appendText('0' . decoct($this->expected)) |
65
|
|
|
->appendText(' mode'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function reportNegativeFailed(FailedMessage $message) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$message->appendText('Expected ') |
74
|
|
|
->appendValue($this->actual) |
75
|
|
|
->appendText(' not to be ') |
76
|
|
|
->appendText('0' . decoct($this->expected)) |
77
|
|
|
->appendText(' mode'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.