Skip to content

Commit 16057ba

Browse files
committed
Add strict types
1 parent c65440a commit 16057ba

19 files changed

+183
-448
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,3 @@ parameters:
88
paths:
99
- src
1010
- tests
11-
ignoreErrors:
12-
# strict-rules errors
13-
- message: "#^Call to function is_string\\(\\) with string will always evaluate to true.#"
14-
count: 1
15-
path: src/XdebugHandler.php

src/PhpConfig.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of composer/xdebug-handler.
57
*
@@ -23,7 +25,7 @@ class PhpConfig
2325
*
2426
* @return string[] Empty array of PHP cli options
2527
*/
26-
public function useOriginal()
28+
public function useOriginal(): array
2729
{
2830
$this->getDataAndReset();
2931
return [];
@@ -34,7 +36,7 @@ public function useOriginal()
3436
*
3537
* @return string[] PHP cli options
3638
*/
37-
public function useStandard()
39+
public function useStandard(): array
3840
{
3941
$data = $this->getDataAndReset();
4042
if ($data !== null) {
@@ -49,7 +51,7 @@ public function useStandard()
4951
*
5052
* @return string[] Empty array of PHP cli options
5153
*/
52-
public function usePersistent()
54+
public function usePersistent(): array
5355
{
5456
$data = $this->getDataAndReset();
5557
if ($data !== null) {
@@ -63,10 +65,9 @@ public function usePersistent()
6365
/**
6466
* Returns restart data if available and resets the environment
6567
*
66-
* @return array|null
6768
* @phpstan-return restartData|null
6869
*/
69-
private function getDataAndReset()
70+
private function getDataAndReset(): ?array
7071
{
7172
$data = XdebugHandler::getRestartSettings();
7273
if ($data !== null) {
@@ -82,10 +83,8 @@ private function getDataAndReset()
8283
*
8384
* @param string $name
8485
* @param string|false $value
85-
*
86-
* @return void
8786
*/
88-
private function updateEnv($name, $value)
87+
private function updateEnv(string $name, $value): void
8988
{
9089
Process::setEnv($name, false !== $value ? $value : null);
9190
}

src/Process.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12+
declare(strict_types=1);
13+
1214
namespace Composer\XdebugHandler;
1315

1416
use Composer\Pcre\Preg;
@@ -27,12 +29,10 @@ class Process
2729
* MIT Licensed (c) John Stevenson <john-stevenson@blueyonder.co.uk>
2830
*
2931
* @param string $arg The argument to be escaped
30-
* @param bool $meta Additionally escape cmd.exe meta characters
32+
* @param bool $meta Additionally escape cmd.exe meta characters
3133
* @param bool $module The argument is the module to invoke
32-
*
33-
* @return string The escaped argument
3434
*/
35-
public static function escape($arg, $meta = true, $module = false)
35+
public static function escape(string $arg, bool $meta = true, bool $module = false): string
3636
{
3737
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
3838
return "'".str_replace("'", "'\\''", $arg)."'";
@@ -67,10 +67,8 @@ public static function escape($arg, $meta = true, $module = false)
6767
* Escapes an array of arguments that make up a shell command
6868
*
6969
* @param string[] $args Argument list, with the module name first
70-
*
71-
* @return string The escaped command line
7270
*/
73-
public static function escapeShellCommand(array $args)
71+
public static function escapeShellCommand(array $args): string
7472
{
7573
$command = '';
7674
$module = array_shift($args);
@@ -90,11 +88,9 @@ public static function escapeShellCommand(array $args)
9088
* Makes putenv environment changes available in $_SERVER and $_ENV
9189
*
9290
* @param string $name
93-
* @param string|null $value A null value unsets the variable
94-
*
95-
* @return bool Whether the environment variable was set
96-
*/
97-
public static function setEnv($name, $value = null)
91+
* @param ?string $value A null value unsets the variable
92+
*/
93+
public static function setEnv(string $name, ?string $value = null): bool
9894
{
9995
$unset = null === $value;
10096

src/Status.php

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12+
declare(strict_types=1);
13+
1214
namespace Composer\XdebugHandler;
1315

1416
use Psr\Log\LoggerInterface;
@@ -48,12 +50,10 @@ class Status
4850
private $time;
4951

5052
/**
51-
* Constructor
52-
*
5353
* @param string $envAllowXdebug Prefixed _ALLOW_XDEBUG name
5454
* @param bool $debug Whether debug output is required
5555
*/
56-
public function __construct($envAllowXdebug, $debug)
56+
public function __construct(string $envAllowXdebug, bool $debug)
5757
{
5858
$start = getenv(self::ENV_RESTART);
5959
Process::setEnv(self::ENV_RESTART);
@@ -65,25 +65,21 @@ public function __construct($envAllowXdebug, $debug)
6565
}
6666

6767
/**
68-
* @param LoggerInterface $logger
68+
* Activates status message output to a PSR3 logger
6969
*
7070
* @return void
7171
*/
72-
public function setLogger(LoggerInterface $logger)
72+
public function setLogger(LoggerInterface $logger): void
7373
{
7474
$this->logger = $logger;
7575
}
7676

7777
/**
7878
* Calls a handler method to report a message
7979
*
80-
* @param string $op The handler constant
81-
* @param null|string $data Data required by the handler
82-
*
83-
* @return void
8480
* @throws \InvalidArgumentException If $op is not known
8581
*/
86-
public function report($op, $data)
82+
public function report(string $op, ?string $data): void
8783
{
8884
if ($this->logger !== null || $this->debug) {
8985
$callable = [$this, 'report'.$op];
@@ -99,13 +95,8 @@ public function report($op, $data)
9995

10096
/**
10197
* Outputs a status message
102-
*
103-
* @param string $text
104-
* @param string $level
105-
*
106-
* @return void
10798
*/
108-
private function output($text, $level = null)
99+
private function output(string $text, ?string $level = null): void
109100
{
110101
if ($this->logger !== null) {
111102
$this->logger->log($level !== null ? $level: LogLevel::DEBUG, $text);
@@ -117,70 +108,64 @@ private function output($text, $level = null)
117108
}
118109

119110
/**
120-
* @param string $loaded
121-
*
122-
* @return void
111+
* Checking status message
123112
*/
124-
private function reportCheck($loaded)
113+
private function reportCheck(string $loaded): void
125114
{
126115
list($version, $mode) = explode('|', $loaded);
127116

128117
if ($version !== '') {
129-
$this->loaded = '('.$version.')'.($mode !== '' ? ' mode='.$mode : '');
118+
$this->loaded = '('.$version.')'.($mode !== '' ? ' xdebug.mode='.$mode : '');
130119
}
131120
$this->modeOff = $mode === 'off';
132121
$this->output('Checking '.$this->envAllowXdebug);
133122
}
134123

135124
/**
136-
* @param string $error
137-
*
138-
* @return void
125+
* Error status message
139126
*/
140-
private function reportError($error)
127+
private function reportError(string $error): void
141128
{
142129
$this->output(sprintf('No restart (%s)', $error), LogLevel::WARNING);
143130
}
144131

145132
/**
146-
* @param string $info
147-
*
148-
* @return void
133+
* Info status message
149134
*/
150-
private function reportInfo($info)
135+
private function reportInfo(string $info): void
151136
{
152137
$this->output($info);
153138
}
154139

155140
/**
156-
* @return void
141+
* No restart status message
157142
*/
158-
private function reportNoRestart()
143+
private function reportNoRestart(): void
159144
{
160145
$this->output($this->getLoadedMessage());
161146

162147
if ($this->loaded !== null) {
163148
$text = sprintf('No restart (%s)', $this->getEnvAllow());
164149
if (!((bool) getenv($this->envAllowXdebug))) {
165-
$text .= ' Allowed by '.($this->modeOff ? 'mode' : 'application');
150+
$text .= ' Allowed by '.($this->modeOff ? 'xdebug.mode' : 'application');
166151
}
167152
$this->output($text);
168153
}
169154
}
170155

171156
/**
172-
* @return void
157+
* Restart status message
173158
*/
174-
private function reportRestart()
159+
private function reportRestart(): void
175160
{
176161
$this->output($this->getLoadedMessage());
177162
Process::setEnv(self::ENV_RESTART, (string) microtime(true));
178163
}
179164

180165
/**
181-
* @return void
166+
* Restarted status message
182167
*/
183-
private function reportRestarted()
168+
private function reportRestarted(): void
184169
{
185170
$loaded = $this->getLoadedMessage();
186171
$text = sprintf('Restarted (%d ms). %s', $this->time, $loaded);
@@ -189,11 +174,9 @@ private function reportRestarted()
189174
}
190175

191176
/**
192-
* @param string $command
193-
*
194-
* @return void
177+
* Restarting status message
195178
*/
196-
private function reportRestarting($command)
179+
private function reportRestarting(string $command): void
197180
{
198181
$text = sprintf('Process restarting (%s)', $this->getEnvAllow());
199182
$this->output($text);
@@ -203,20 +186,16 @@ private function reportRestarting($command)
203186

204187
/**
205188
* Returns the _ALLOW_XDEBUG environment variable as name=value
206-
*
207-
* @return string
208189
*/
209-
private function getEnvAllow()
190+
private function getEnvAllow(): string
210191
{
211192
return $this->envAllowXdebug.'='.getenv($this->envAllowXdebug);
212193
}
213194

214195
/**
215196
* Returns the Xdebug status and version
216-
*
217-
* @return string
218197
*/
219-
private function getLoadedMessage()
198+
private function getLoadedMessage(): string
220199
{
221200
$loaded = $this->loaded !== null ? sprintf('loaded %s', $this->loaded) : 'not loaded';
222201
return 'The Xdebug extension is '.$loaded;

0 commit comments

Comments
 (0)