Skip to content

Commit b59ed11

Browse files
committed
Add type hints #10
1 parent af5976c commit b59ed11

File tree

6 files changed

+80
-241
lines changed

6 files changed

+80
-241
lines changed

src/WpTestsStarter/Helper/SaltGenerator.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ class SaltGenerator
88
{
99
/**
1010
* Generates random strings using all ascii chars between
11-
* 32 (Space) and 126 (~)
12-
*
13-
* @param int $length
14-
* @return string
11+
* 32 (Space) and 126 (~). Note that this is all but secure and
12+
* is only meant to be used during automated tests
1513
*/
16-
public function generateSalt($length = 40)
14+
public function generateSalt(int $length = 40): string
1715
{
1816
$salt = '';
1917
for ($i = 1; $i <= $length; $i++) {

src/WpTestsStarter/WpTestsStarter.php

+48-139
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,26 @@
44

55
namespace WpTestsStarter;
66

7+
use WpTestsStarter\Helper\SaltGenerator;
8+
79
class WpTestsStarter
810
{
9-
/**
10-
* @type string
11-
*/
12-
private $baseDir;
11+
private string $baseDir;
1312

14-
/**
15-
* @type Helper\SaltGenerator
16-
*/
17-
private $saltGenerator;
13+
private SaltGenerator $saltGenerator;
1814

1915
/**
20-
* list of all by this class defined constants
21-
*
22-
* @type array
16+
* @type string[]
2317
*/
24-
private $definedConstants = [];
18+
private array $definedConstants = [];
2519

2620
/**
27-
* Pass the absolute path of the wordpress-dev package here.
28-
* It is "$baseDir/vendor/inpsyde/wordpress-dev" if you're using
29-
* the inpsyde/wordpress-dev package
30-
*
31-
* @param string $baseDir
32-
* @param Helper\SaltGenerator $saltGenerator
21+
* @param string $baseDir Absolute path to the wordpress-develop repository
3322
*/
34-
public function __construct($baseDir, Helper\SaltGenerator $saltGenerator = null)
23+
public function __construct(string $baseDir, ?SaltGenerator $saltGenerator = null)
3524
{
3625
$this->baseDir = rtrim($baseDir, '\\/');
37-
if (!$saltGenerator) {
38-
$saltGenerator = new Helper\SaltGenerator();
39-
}
40-
$this->saltGenerator = $saltGenerator;
26+
$this->saltGenerator = $saltGenerator ?? new SaltGenerator();
4127
}
4228

4329
/**
@@ -67,92 +53,63 @@ public function bootstrap()
6753
}
6854

6955
/**
70-
* Define a given constant if it not already exists
71-
*
72-
* @param string $const
7356
* @param mixed $value
74-
* @return bool
7557
*/
76-
public function defineConst($const, $value)
58+
public function defineConst(string $const, $value): bool
7759
{
7860
if (defined($const)) {
61+
7962
return false;
8063
}
8164

8265
$this->definedConstants[$const] = $value;
66+
8367
return define($const, $value);
8468
}
8569

86-
/**
87-
* @param string $abspath
88-
*/
89-
public function defineAbspath($abspath = '')
70+
public function defineAbspath(?string $abspath = null): void
9071
{
9172
if (empty($abspath)) {
9273
$abspath = $this->baseDir . '/src/';
9374
}
9475
$this->defineConst('ABSPATH', $abspath);
9576
}
9677

97-
/**
98-
* @param string $dbName
99-
*/
100-
public function defineDbName($dbName)
78+
public function defineDbName(string $dbName): void
10179
{
10280
$this->defineConst('DB_NAME', $dbName);
10381
}
10482

105-
/**
106-
* @param string $dbHost
107-
*/
108-
public function defineDbHost($dbHost = 'localhost')
83+
public function defineDbHost(string $dbHost = 'localhost'): void
10984
{
11085
$this->defineConst('DB_HOST', $dbHost);
11186
}
11287

113-
/**
114-
* @param string $dbUser
115-
*/
116-
public function defineDbUser($dbUser)
88+
public function defineDbUser(string $dbUser): void
11789
{
11890
$this->defineConst('DB_USER', $dbUser);
11991
}
12092

121-
/**
122-
* @param string $dbPassword
123-
*/
124-
public function defineDbPassword($dbPassword)
93+
public function defineDbPassword(string $dbPassword): void
12594
{
12695
$this->defineConst('DB_PASSWORD', $dbPassword);
12796
}
12897

129-
/**
130-
* @param string $dbCharset
131-
*/
132-
public function defineDbCharset($dbCharset = 'utf8')
98+
public function defineDbCharset(string $dbCharset = 'utf8'): void
13399
{
134100
$this->defineConst('DB_CHARSET', $dbCharset);
135101
}
136102

137-
/**
138-
* @param string $dbCollate
139-
*/
140-
public function defineDbCollate($dbCollate = '')
103+
public function defineDbCollate(string $dbCollate = ''): void
141104
{
142105
$this->defineConst('DB_COLLATE', $dbCollate);
143106
}
144107

145-
/**
146-
* @param bool $wpDebug
147-
*/
148-
public function defineWpDebug($wpDebug = false)
108+
public function defineWpDebug(bool $wpDebug = false): void
149109
{
150-
$this->defineConst('WP_DEBUG', (bool)$wpDebug);
110+
$this->defineConst('WP_DEBUG', $wpDebug);
151111
}
152112

153-
/**
154-
* define the security keys and salts
155-
*/
156113
public function defineSalts()
157114
{
158115
$saltConstants = [
@@ -174,83 +131,57 @@ public function defineSalts()
174131
}
175132
}
176133

177-
/**
178-
* @param string $domain
179-
*/
180-
public function defineTestsDomain($domain = 'example.org')
134+
public function defineTestsDomain(string $domain = 'example.org'): void
181135
{
182136
$this->defineConst('WP_TESTS_DOMAIN', $domain);
183137
}
184138

185-
/**
186-
* @param string $email
187-
*/
188-
public function defineTestsEmail($email = '[email protected]')
139+
public function defineTestsEmail(string $email = '[email protected]'): void
189140
{
190141
$this->defineConst('WP_TESTS_EMAIL', $email);
191142
}
192143

193-
/**
194-
* @param string $title
195-
*/
196-
public function defineTestsTitle($title = 'Test Blog')
144+
public function defineTestsTitle(string $title = 'Test Blog'): void
197145
{
198146
$this->defineConst('WP_TESTS_TITLE', $title);
199147
}
200148

201-
/**
202-
* @param string $binary
203-
*/
204-
public function definePhpBinary($binary = 'php')
149+
public function definePhpBinary(string $binary = 'php'): void
205150
{
206151
$this->defineConst('WP_PHP_BINARY', $binary);
207152
}
208153

209-
/**
210-
* @param string $lang
211-
*/
212-
public function defineWpLang($lang = '')
154+
public function defineWpLang(string $lang = ''): void
213155
{
214156
$this->defineConst('WPLANG', $lang);
215157
}
216158

217-
/**
218-
* @param bool $flag
219-
*/
220-
public function defineTestForceKnownBugs($flag)
159+
public function defineTestForceKnownBugs(bool $flag): void
221160
{
222-
$this->defineConst('WP_TESTS_FORCE_KNOWN_BUGS', (bool)$flag);
161+
$this->defineConst('WP_TESTS_FORCE_KNOWN_BUGS', $flag);
223162
}
224163

225-
/**
226-
* @param $flag
227-
*/
228-
public function defineTestMultisite($flag)
164+
public function defineTestMultisite(bool $flag): void
229165
{
230-
$this->defineConst('WP_TESTS_MULTISITE', (bool)$flag);
166+
$this->defineConst('WP_TESTS_MULTISITE', $flag);
231167
}
232168

233-
/**
234-
* @param string $dir
235-
*/
236-
public function defineWpPluginDir($dir)
169+
public function defineWpPluginDir(string $dir): void
237170
{
238171
$dir = rtrim($dir, '\\/');
239172
$this->defineConst('WP_PLUGIN_DIR', $dir);
240173
}
241174

242175
/**
243-
* pass a plugin slug like 'directory/plugin-file.php'
244-
*
245-
* @param string $plugin
176+
* @param string $plugin a plugin file relative to WP's plugin directory like 'directory/plugin-file.php'
246177
*/
247-
public function setActivePlugin($plugin)
178+
public function setActivePlugin(string $plugin): void
248179
{
249-
if (!isset($GLOBALS['wp_tests_options'])) {
180+
if (! isset($GLOBALS['wp_tests_options'])) {
250181
$GLOBALS['wp_tests_options'] = [];
251182
}
252183

253-
if (!isset($GLOBALS['wp_tests_options']['active_plugins'])) {
184+
if (! isset($GLOBALS['wp_tests_options']['active_plugins'])) {
254185
$GLOBALS['wp_tests_options']['active_plugins'] = [];
255186
}
256187

@@ -261,43 +192,30 @@ public function setActivePlugin($plugin)
261192
$GLOBALS['wp_tests_options']['active_plugins'][] = $plugin;
262193
}
263194

264-
/**
265-
* @param string $prefix
266-
*/
267-
public function setTablePrefix($prefix = 'wptests_')
195+
public function setTablePrefix(string $prefix = 'wptests_'): void
268196
{
269197
$var = 'table_prefix';
270198
$this->setGlobal($var, $prefix);
271199
}
272200

273201
/**
274-
* @param $var
275-
* @param $value
202+
* @param mixed $value
276203
*/
277-
public function setGlobal($var, $value)
204+
public function setGlobal(string $var, $value): void
278205
{
279206
$GLOBALS[$var] = $value;
280207
}
281208

282-
/**
283-
* the WordPress bootstrap process does not allow
284-
* to define a custom path of the config but looks
285-
* for this file. so we create just an empty one.
286-
*/
287209
public function createDummyConfigFile()
288210
{
289211
$configFile = $this->getConfigFile();
290-
if (!file_exists($configFile)) {
212+
if (! file_exists($configFile)) {
291213
touch($configFile);
292214
}
293215

294216
/**
295-
* the WordPress testing bootstrap requires the definitions
296-
* of all these content in exactly this file, there's no way
297-
* to dynamically define these constants as
298-
* tests/phpunit/includes/bootstrap.php triggers a system() call to
299-
* tests/phpunit/includes/install.php with a static path to the
300-
* config file
217+
* We have to persist all dynamic configuration (constants and globals) in a wp-config.php
218+
* as the WordPress internal boostrap process runs a sub process for the installation (setup DB tables)
301219
*/
302220
$constantsDefinition = $this->getDefinedConstantsCode();
303221
$content = <<<PHP
@@ -309,26 +227,20 @@ public function createDummyConfigFile()
309227
file_put_contents($configFile, $content, LOCK_EX);
310228
}
311229

312-
/**
313-
* @return string
314-
*/
315-
public function getConfigFile()
230+
public function getConfigFile(): string
316231
{
317232
return $this->baseDir . '/wp-tests-config.php';
318233
}
319234

320-
/**
321-
* @return array
322-
*/
323-
public function getDefinedConstants()
235+
/**
236+
* @return string[]
237+
*/
238+
public function getDefinedConstants(): array
324239
{
325240
return $this->definedConstants;
326241
}
327242

328-
/**
329-
* that feels so ugly
330-
*/
331-
public function getDefinedConstantsCode()
243+
public function getDefinedConstantsCode(): string
332244
{
333245
$code = '';
334246
foreach ($this->definedConstants as $constant => $value) {
@@ -341,10 +253,7 @@ public function getDefinedConstantsCode()
341253
return $code;
342254
}
343255

344-
/**
345-
* that feels even more ugly
346-
*/
347-
public function escapePhpString($value)
256+
public function escapePhpString($value): string
348257
{
349258
$value = str_replace(
350259
['<?php', '<?', '?>'],

0 commit comments

Comments
 (0)