Skip to content

Commit 6ef3d4b

Browse files
committed
minor improvements
1 parent 2e34e03 commit 6ef3d4b

2 files changed

Lines changed: 22 additions & 108 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "LGPL-3.0",
66
"require": {
77
"php": "^7.1.3",
8-
"symfony/process": "^4.0|5.0"
8+
"symfony/process": "^4.2|5.0"
99
},
1010
"require-dev": {
1111
"phpunit/phpunit": "^7.5",

src/Php.php

Lines changed: 21 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -16,93 +16,54 @@ class Php
1616

1717
public function __construct()
1818
{
19-
$this->setTempDirectory(\sys_get_temp_dir());
19+
$this->tempDirectory = \sys_get_temp_dir();
2020
}
2121

22-
/**
23-
* @param string $resultCharset
24-
*
25-
* @return Php
26-
*/
27-
public function setResultCharset($resultCharset)
22+
public function setResultCharset(string $resultCharset): self
2823
{
2924
$this->resultCharset = $resultCharset;
3025
return $this;
3126
}
3227

33-
/**
34-
* @return string
35-
*/
36-
public function getResultCharset()
28+
public function getResultCharset(): string
3729
{
3830
return $this->resultCharset;
3931
}
4032

41-
/**
42-
* @param string $sourceCharset
43-
*
44-
* @return Php
45-
*/
46-
public function setSourceCharset($sourceCharset)
33+
public function setSourceCharset(?string $sourceCharset): self
4734
{
4835
$this->sourceCharset = $sourceCharset;
4936
return $this;
5037
}
5138

52-
/**
53-
* @return string|null
54-
*/
55-
public function getSourceCharset()
39+
public function getSourceCharset(): ?string
5640
{
5741
return $this->sourceCharset;
5842
}
5943

60-
61-
/**
62-
* @param string $path
63-
* @return Php
64-
*/
65-
public function setCli($path)
44+
public function setCli(string $path): self
6645
{
6746
$this->cli = $path;
6847
return $this;
6948
}
7049

71-
/**
72-
* @return string
73-
*/
74-
public function getCli()
50+
public function getCli(): string
7551
{
7652
return $this->cli;
7753
}
7854

79-
80-
/**
81-
* @param string $path
82-
* @return Php
83-
*/
84-
public function setTempDirectory($path)
55+
public function setTempDirectory(string $path): self
8556
{
8657
$this->tempDirectory = $path;
8758
return $this;
8859
}
8960

90-
/**
91-
* @return string|null
92-
*/
93-
public function getTempDirectory()
61+
public function getTempDirectory(): string
9462
{
9563
return $this->tempDirectory;
9664
}
9765

98-
99-
/**
100-
* @param string $source
101-
*
102-
* @throws \Exception
103-
* @return array
104-
*/
105-
public function check($source)
66+
public function check(string $source): array
10667
{
10768
$file = \tempnam($this->getTempDirectory(), 'syntax');
10869
if (false === $file) {
@@ -124,7 +85,7 @@ public function check($source)
12485

12586
try {
12687
$result = $this->checkFile($file);
127-
} catch (\Exception $e) {
88+
} catch (\Throwable $e) {
12889
\unlink($file);
12990
throw $e;
13091
}
@@ -134,12 +95,7 @@ public function check($source)
13495
return $this->formatCheckOutput($result);
13596
}
13697

137-
138-
/**
139-
* @param array $result
140-
* @return array
141-
*/
142-
protected function formatCheckOutput(array $result)
98+
protected function formatCheckOutput(array $result): array
14399
{
144100
if (isset($result['errors'])) {
145101
\array_walk($result['errors'], static function (&$item) {
@@ -150,16 +106,9 @@ protected function formatCheckOutput(array $result)
150106
return $result;
151107
}
152108

153-
154-
/**
155-
* @param string $file
156-
*
157-
* @throws \Exception
158-
* @return array
159-
*/
160-
public function checkFile($file)
109+
public function checkFile(string $file): array
161110
{
162-
$result = $this->execute(new Process([$this->getCli(), '-l', $file]));
111+
$result = $this->execute($file);
163112

164113
if (0 === $result['code']) {
165114
return [
@@ -170,7 +119,7 @@ public function checkFile($file)
170119

171120
$fullMessage = \preg_replace('/ in (?:.+) on line (?:[0-9]+)$/', '', $result['output']);
172121
\preg_match('/ on line ([0-9]+)$/', $result['output'], $matchLine);
173-
$line = isset($matchLine[1]) ? \intval($matchLine[1]) : null;
122+
$line = isset($matchLine[1]) ? (int)($matchLine[1]) : null;
174123

175124
[$type, $message] = \explode(': ', $fullMessage);
176125

@@ -188,12 +137,7 @@ public function checkFile($file)
188137
];
189138
}
190139

191-
192-
/**
193-
* @param string $message
194-
* @return string
195-
*/
196-
protected function convertMessage($message)
140+
protected function convertMessage(string $message): string
197141
{
198142
if (null !== $this->getSourceCharset()) {
199143
return \mb_convert_encoding($message, $this->getResultCharset(), $this->getSourceCharset());
@@ -202,14 +146,9 @@ protected function convertMessage($message)
202146
return $message;
203147
}
204148

205-
206-
/**
207-
* @param Process $process
208-
* @throws \Exception
209-
* @return array
210-
*/
211-
protected function execute(Process $process)
149+
protected function execute(string $file): array
212150
{
151+
$process = new Process([$this->getCli(), '-l', $file]);
213152
$process->run();
214153

215154
if ($process->isSuccessful()) {
@@ -230,31 +169,12 @@ protected function execute(Process $process)
230169
];
231170
}
232171

233-
234-
/**
235-
* @param string $source
236-
* @param int $line
237-
* @param string $cssCodeClass
238-
* @param string $cssCodeCorrectLineClass
239-
* @param string $cssCodeIncorrectLineClass
240-
*
241-
* @return string
242-
*/
243-
public static function formatOutputHelper($source, $line, $cssCodeClass = 'syntax-code', $cssCodeCorrectLineClass = 'syntax-correct-line', $cssCodeIncorrectLineClass = 'syntax-incorrect-line')
172+
public static function formatOutputHelper(string $source, int $line, string $cssCodeClass = 'syntax-code', string $cssCodeCorrectLineClass = 'syntax-correct-line', string $cssCodeIncorrectLineClass = 'syntax-incorrect-line'): string
244173
{
245174
return '<div class="' . \htmlspecialchars($cssCodeClass) . '"><pre><code>' . self::formatCode($source, $line, $cssCodeCorrectLineClass, $cssCodeIncorrectLineClass) . '</code></pre></div>';
246175
}
247176

248-
249-
/**
250-
* @param string $source
251-
* @param int $line
252-
* @param string $cssCodeCorrectLineClass
253-
* @param string $cssCodeIncorrectLineClass
254-
*
255-
* @return string
256-
*/
257-
protected static function formatCode($source, $line, $cssCodeCorrectLineClass, $cssCodeIncorrectLineClass)
177+
protected static function formatCode(string $source, int $line, string $cssCodeCorrectLineClass = 'syntax-correct-line', string $cssCodeIncorrectLineClass = 'syntax-incorrect-line'): string
258178
{
259179
$array = self::formatXhtmlHighlight($source);
260180
$all = \count($array);
@@ -269,13 +189,7 @@ protected static function formatCode($source, $line, $cssCodeCorrectLineClass, $
269189
return $page;
270190
}
271191

272-
273-
/**
274-
* @param string $source
275-
*
276-
* @return array
277-
*/
278-
protected static function formatXhtmlHighlight($source)
192+
protected static function formatXhtmlHighlight(string $source): array
279193
{
280194
return \array_slice(
281195
\explode(

0 commit comments

Comments
 (0)