1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node\Expr\Array_; |
6
|
|
|
use PhpParser\Node\Expr\ArrayItem; |
7
|
|
|
use PhpParser\Node\Scalar\LNumber; |
8
|
|
|
use PhpParser\Node\Scalar\String_; |
9
|
|
|
use PhpParser\Node\Stmt\Return_; |
10
|
|
|
use PhpParser\PrettyPrinter\Standard; |
11
|
|
|
|
12
|
|
|
use UCD\Exception\UnexpectedValueException; |
13
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File; |
14
|
|
|
|
15
|
|
|
class PHPFile implements File |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \SplFileInfo |
19
|
|
|
*/ |
20
|
|
|
private $fileInfo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \SplFileInfo $fileInfo |
24
|
|
|
*/ |
25
|
|
|
public function __construct(\SplFileInfo $fileInfo) |
26
|
|
|
{ |
27
|
|
|
$this->fileInfo = $fileInfo; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string[] |
32
|
|
|
* @throws UnexpectedValueException |
33
|
|
|
*/ |
34
|
|
|
public function readArray() |
35
|
|
|
{ |
36
|
|
|
$value = $this->readFile(); |
37
|
|
|
$this->verifyType($value, 'array'); |
38
|
|
|
|
39
|
|
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
private function readFile() |
46
|
|
|
{ |
47
|
|
|
$pathname = $this->fileInfo->getPathname(); |
48
|
|
|
|
49
|
|
|
return require $this->isZippedFile() ? 'compress.zlib://' . $pathname : $pathname; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $value |
54
|
|
|
* @param string $expectedType |
55
|
|
|
* @throws UnexpectedValueException |
56
|
|
|
*/ |
57
|
|
|
private function verifyType($value, $expectedType) |
58
|
|
|
{ |
59
|
|
|
$actualType = gettype($value); |
60
|
|
|
|
61
|
|
|
if ($actualType !== $expectedType) { |
62
|
|
|
throw new UnexpectedValueException(sprintf('Expected %s, got %s', $expectedType, $actualType)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string[] $items |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function writeArray(array $items) |
71
|
|
|
{ |
72
|
|
|
$content = $this->generateFileContentForArray($items); |
73
|
|
|
|
74
|
|
|
$fileInfo = $this->isZippedFile() |
75
|
|
|
? new \SplFileInfo('compress.zlib://' . $this->fileInfo->getPathname()) |
76
|
|
|
: $this->fileInfo; |
77
|
|
|
$file = $fileInfo->openFile('w'); |
78
|
|
|
$file->fwrite($content); |
79
|
|
|
|
80
|
|
|
return $file->fflush(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $items |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function generateFileContentForArray(array $items) |
88
|
|
|
{ |
89
|
|
|
$nodes = []; |
90
|
|
|
|
91
|
|
|
foreach ($items as $key => $value) { |
92
|
|
|
$key = is_int($key) ? new LNumber($key) : new String_($key); |
93
|
|
|
$value = new String_($value); |
94
|
|
|
$node = new ArrayItem($value, $key); |
95
|
|
|
array_push($nodes, $node); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$statements = [new Return_(new Array_($nodes))]; |
99
|
|
|
$printer = new Standard(); |
100
|
|
|
|
101
|
|
|
return $printer->prettyPrintFile($statements); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \SplFileInfo |
106
|
|
|
*/ |
107
|
|
|
public function getInfo() |
108
|
|
|
{ |
109
|
|
|
return $this->fileInfo; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $items |
|
|
|
|
114
|
|
|
* @return boolean |
115
|
|
|
*/ |
116
|
|
|
private function isZippedFile() |
117
|
|
|
{ |
118
|
|
|
if (preg_match('`^phpvfs[0-9a-z]*://`i', $this->fileInfo->getPathname())) { |
119
|
|
|
// php-vfs does not support combining stream wrappers |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->fileInfo->getExtension() === 'gz'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.