Skip to content

Commit a92a703

Browse files
committed
ISSUE-337: typehint + style
1 parent 4e88ae1 commit a92a703

24 files changed

+111
-84
lines changed

src/Composer/ModuleFinder.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace PhpList\Core\Composer;
56

7+
use InvalidArgumentException;
68
use Symfony\Component\Yaml\Yaml;
79

810
/**
@@ -20,14 +22,14 @@ class ModuleFinder
2022
/**
2123
* @var PackageRepository
2224
*/
23-
private $packageRepository = null;
25+
private ?PackageRepository $packageRepository = null;
2426

2527
/**
2628
* @param PackageRepository $repository
2729
*
2830
* @return void
2931
*/
30-
public function injectPackageRepository(PackageRepository $repository)
32+
public function injectPackageRepository(PackageRepository $repository): void
3133
{
3234
$this->packageRepository = $repository;
3335
}
@@ -38,7 +40,7 @@ public function injectPackageRepository(PackageRepository $repository)
3840
* @return string[][] class names of the bundles of all installed phpList modules:
3941
* ['module package name' => ['bundle class name 1', 'bundle class name 2']]
4042
*
41-
* @throws \InvalidArgumentException
43+
* @throws InvalidArgumentException
4244
*/
4345
public function findBundleClasses(): array
4446
{
@@ -66,9 +68,9 @@ public function findBundleClasses(): array
6668
*
6769
* @return void
6870
*
69-
* @throws \InvalidArgumentException if $extra has an invalid bundles configuration
71+
* @throws InvalidArgumentException if $extra has an invalid bundles configuration
7072
*/
71-
private function validateBundlesSectionInExtra(array $extra)
73+
private function validateBundlesSectionInExtra(array $extra): void
7274
{
7375
if (!isset($extra['phplist/core'])) {
7476
return;
@@ -79,7 +81,7 @@ private function validateBundlesSectionInExtra(array $extra)
7981
return;
8082
}
8183
if (!is_array($extra['phplist/core']['bundles'])) {
82-
throw new \InvalidArgumentException(
84+
throw new InvalidArgumentException(
8385
'The extras.phplist/core.bundles section in the composer.json must be an array.',
8486
1505411665
8587
);
@@ -89,7 +91,7 @@ private function validateBundlesSectionInExtra(array $extra)
8991
$bundleExtras = $extra['phplist/core']['bundles'];
9092
foreach ($bundleExtras as $key => $bundleName) {
9193
if (!is_string($bundleName)) {
92-
throw new \InvalidArgumentException(
94+
throw new InvalidArgumentException(
9395
'The extras.phplist/core.bundles. ' . $key . '" section in the composer.json must be a string.',
9496
1505412184
9597
);
@@ -104,12 +106,12 @@ private function validateBundlesSectionInExtra(array $extra)
104106
*
105107
* @return void
106108
*
107-
* @throws \InvalidArgumentException
109+
* @throws InvalidArgumentException
108110
*/
109-
private function validatePhpListSectionInExtra(array $extra)
111+
private function validatePhpListSectionInExtra(array $extra): void
110112
{
111113
if (!is_array($extra['phplist/core'])) {
112-
throw new \InvalidArgumentException(
114+
throw new InvalidArgumentException(
113115
'The extras.phplist/core" section in the composer.json must be an array.',
114116
1505411436
115117
);
@@ -125,7 +127,7 @@ private function validatePhpListSectionInExtra(array $extra)
125127
*/
126128
public function createBundleConfigurationYaml(): string
127129
{
128-
return static::YAML_COMMENT . "\n" . Yaml::dump($this->findBundleClasses());
130+
return static::YAML_COMMENT . PHP_EOL . Yaml::dump($this->findBundleClasses());
129131
}
130132

131133
/**
@@ -134,7 +136,7 @@ public function createBundleConfigurationYaml(): string
134136
* @return array[] class names of the routes of all installed phpList modules:
135137
* ['route name' => [route configuration]
136138
*
137-
* @throws \InvalidArgumentException
139+
* @throws InvalidArgumentException
138140
*/
139141
public function findRoutes(): array
140142
{
@@ -167,9 +169,9 @@ public function findRoutes(): array
167169
*
168170
* @return void
169171
*
170-
* @throws \InvalidArgumentException if $extra has an invalid routes configuration
172+
* @throws InvalidArgumentException if $extra has an invalid routes configuration
171173
*/
172-
private function validateRoutesSectionInExtra(array $extra)
174+
private function validateRoutesSectionInExtra(array $extra): void
173175
{
174176
if (!isset($extra['phplist/core'])) {
175177
return;
@@ -180,7 +182,7 @@ private function validateRoutesSectionInExtra(array $extra)
180182
return;
181183
}
182184
if (!is_array($extra['phplist/core']['routes'])) {
183-
throw new \InvalidArgumentException(
185+
throw new InvalidArgumentException(
184186
'The extras.phplist/core.routes section in the composer.json must be an array.',
185187
1506429004
186188
);
@@ -190,7 +192,7 @@ private function validateRoutesSectionInExtra(array $extra)
190192
$bundleExtras = $extra['phplist/core']['routes'];
191193
foreach ($bundleExtras as $routeName => $routeConfiguration) {
192194
if (!is_array($routeConfiguration)) {
193-
throw new \InvalidArgumentException(
195+
throw new InvalidArgumentException(
194196
'The extras.phplist/core.routes. ' . $routeName .
195197
'" section in the composer.json must be an array.',
196198
1506429860
@@ -208,15 +210,15 @@ private function validateRoutesSectionInExtra(array $extra)
208210
*/
209211
public function createRouteConfigurationYaml(): string
210212
{
211-
return static::YAML_COMMENT . "\n" . Yaml::dump($this->findRoutes());
213+
return static::YAML_COMMENT . PHP_EOL . Yaml::dump($this->findRoutes());
212214
}
213215

214216
/**
215217
* Finds and merges the configuration in all installed modules.
216218
*
217219
* @return array configuration which can be dumped in a config_modules.yml file
218220
*
219-
* @throws \InvalidArgumentException
221+
* @throws InvalidArgumentException
220222
*/
221223
public function findGeneralConfiguration(): array
222224
{
@@ -244,9 +246,9 @@ public function findGeneralConfiguration(): array
244246
*
245247
* @return void
246248
*
247-
* @throws \InvalidArgumentException if $extra has an invalid routes configuration
249+
* @throws InvalidArgumentException if $extra has an invalid routes configuration
248250
*/
249-
private function validateGeneralConfigurationSectionInExtra(array $extra)
251+
private function validateGeneralConfigurationSectionInExtra(array $extra): void
250252
{
251253
if (!isset($extra['phplist/core'])) {
252254
return;
@@ -257,7 +259,7 @@ private function validateGeneralConfigurationSectionInExtra(array $extra)
257259
return;
258260
}
259261
if (!is_array($extra['phplist/core']['configuration'])) {
260-
throw new \InvalidArgumentException(
262+
throw new InvalidArgumentException(
261263
'The extras.phplist/core.configuration section in the composer.json must be an array.',
262264
1508165934
263265
);
@@ -273,6 +275,6 @@ private function validateGeneralConfigurationSectionInExtra(array $extra)
273275
*/
274276
public function createGeneralConfigurationYaml(): string
275277
{
276-
return static::YAML_COMMENT . "\n" . Yaml::dump($this->findGeneralConfiguration());
278+
return static::YAML_COMMENT . PHP_EOL . Yaml::dump($this->findGeneralConfiguration());
277279
}
278280
}

src/Core/ApplicationKernel.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public function registerBundles(): array
3838
*
3939
* @return string absolute path without the trailing slash
4040
*/
41-
public function getProjectDir()
41+
public function getProjectDir(): string
4242
{
4343
return $this->getAndCreateApplicationStructure()->getCorePackageRoot();
4444
}
4545

4646
/**
4747
* @return string
4848
*/
49-
public function getRootDir()
49+
public function getRootDir(): string
5050
{
5151
return $this->getProjectDir();
5252
}
@@ -70,15 +70,15 @@ private function getApplicationDir(): string
7070
/**
7171
* @return string
7272
*/
73-
public function getCacheDir()
73+
public function getCacheDir(): string
7474
{
7575
return $this->getApplicationDir() . '/var/cache/' . $this->getEnvironment();
7676
}
7777

7878
/**
7979
* @return string
8080
*/
81-
public function getLogDir()
81+
public function getLogDir(): string
8282
{
8383
return $this->getApplicationDir() . '/var/logs';
8484
}
@@ -102,7 +102,7 @@ private function getAndCreateApplicationStructure(): ApplicationStructure
102102
*
103103
* @return void
104104
*/
105-
protected function build(ContainerBuilder $container)
105+
protected function build(ContainerBuilder $container): void
106106
{
107107
$container->setParameter('kernel.application_dir', $this->getApplicationDir());
108108
}
@@ -116,7 +116,7 @@ protected function build(ContainerBuilder $container)
116116
*
117117
* @throws Exception
118118
*/
119-
public function registerContainerConfiguration(LoaderInterface $loader)
119+
public function registerContainerConfiguration(LoaderInterface $loader): void
120120
{
121121
$loader->load($this->getApplicationDir() . '/config/parameters.yml');
122122
$loader->load($this->getRootDir() . '/config/config_' . $this->getEnvironment() . '.yml');
@@ -140,7 +140,6 @@ private function bundlesFromConfiguration(): array
140140
{
141141
$bundles = [];
142142

143-
/** @var string[] $packageBundles */
144143
foreach ($this->readBundleConfiguration() as $packageBundles) {
145144
foreach ($packageBundles as $bundleClassName) {
146145
if (class_exists($bundleClassName)) {

src/Core/ApplicationStructure.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace PhpList\Core\Core;
56

7+
use RuntimeException;
8+
69
/**
710
* This class provides information about the current application and its file structure.
811
*
@@ -15,7 +18,7 @@ class ApplicationStructure
1518
*
1619
* @return string the absolute path without the trailing slash.
1720
*
18-
* @throws \RuntimeException if there is no composer.json in the application root
21+
* @throws RuntimeException if there is no composer.json in the application root
1922
*/
2023
public function getCorePackageRoot(): string
2124
{
@@ -33,22 +36,20 @@ public function getCorePackageRoot(): string
3336
*
3437
* @return string the absolute path without the trailing slash
3538
*
36-
* @throws \RuntimeException if there is no composer.json in the application root
39+
* @throws RuntimeException if there is no composer.json in the application root
3740
*/
3841
public function getApplicationRoot(): string
3942
{
4043
$corePackagePath = $this->getCorePackageRoot();
4144
$corePackageIsRootPackage = interface_exists('PhpList\\Core\\Tests\\Support\\Interfaces\\TestMarker');
42-
if ($corePackageIsRootPackage) {
43-
$applicationRoot = $corePackagePath;
44-
} else {
45+
if (!$corePackageIsRootPackage) {
4546
// remove 3 more path segments, i.e., "vendor/phplist/core/"
4647
$corePackagePath = dirname($corePackagePath, 3);
47-
$applicationRoot = $corePackagePath;
4848
}
49+
$applicationRoot = $corePackagePath;
4950

5051
if (!file_exists($applicationRoot . '/composer.json')) {
51-
throw new \RuntimeException(
52+
throw new RuntimeException(
5253
'There is no composer.json in the supposed application root "' . $applicationRoot . '".',
5354
1501169001
5455
);

src/Core/Bootstrap.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ class Bootstrap
3232
/**
3333
* @var Bootstrap|null
3434
*/
35-
private static $instance = null;
35+
private static ?Bootstrap $instance = null;
3636

3737
/**
3838
* @var bool
3939
*/
40-
private $isConfigured = false;
40+
private bool $isConfigured = false;
4141

4242
/**
4343
* @var string
4444
*/
45-
private $environment = Environment::DEFAULT_ENVIRONMENT;
45+
private string $environment = Environment::DEFAULT_ENVIRONMENT;
4646

4747
/**
4848
* @var ApplicationKernel
@@ -52,7 +52,7 @@ class Bootstrap
5252
/**
5353
* @var ApplicationStructure
5454
*/
55-
private $applicationStructure = null;
55+
private ApplicationStructure $applicationStructure;
5656

5757
/**
5858
* Protected constructor to avoid direct instantiation of this class.
@@ -92,7 +92,7 @@ public static function getInstance(): Bootstrap
9292
*
9393
* @return void
9494
*/
95-
public static function purgeInstance()
95+
public static function purgeInstance(): void
9696
{
9797
self::$instance = null;
9898
}
@@ -144,9 +144,9 @@ private function isDebugEnabled(): bool
144144
* @SuppressWarnings("PHPMD.ExitExpression")
145145
* @SuppressWarnings("PHPMD.Superglobals")
146146
*
147-
* @return Bootstrap|null fluent interface
147+
* @return Bootstrap fluent interface
148148
*/
149-
public function ensureDevelopmentOrTestingEnvironment()
149+
public function ensureDevelopmentOrTestingEnvironment(): static
150150
{
151151
$usesProxy = isset($_SERVER['HTTP_CLIENT_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR']);
152152
$isOnCli = PHP_SAPI === 'cli' || PHP_SAPI === 'cli-server';
@@ -181,7 +181,7 @@ public function configure(): Bootstrap
181181
*
182182
* @throws RuntimeException if configure has not been called before
183183
*/
184-
private function assertConfigureHasBeenCalled()
184+
private function assertConfigureHasBeenCalled(): void
185185
{
186186
if (!$this->isConfigured) {
187187
throw new RuntimeException('Please call configure() first.', 1501170550);

src/Core/Environment.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace PhpList\Core\Core;
56

7+
use UnexpectedValueException;
8+
69
/**
710
* This class provides methods and constants for the application environment/context.
811
*
@@ -39,7 +42,7 @@ final class Environment
3942
/**
4043
* @var string[]
4144
*/
42-
private static $validEnvironments = [self::PRODUCTION, self::DEVELOPMENT, self::TESTING];
45+
private static array $validEnvironments = [self::PRODUCTION, self::DEVELOPMENT, self::TESTING];
4346

4447
/**
4548
* Private constructor to avoid instantiation.
@@ -55,13 +58,13 @@ private function __construct()
5558
*
5659
* @return void
5760
*
58-
* @throws \UnexpectedValueException
61+
* @throws UnexpectedValueException
5962
*/
60-
public static function validateEnvironment(string $environment)
63+
public static function validateEnvironment(string $environment): void
6164
{
6265
if (!in_array($environment, self::$validEnvironments, true)) {
6366
$environmentsText = '"' . implode('", ', self::$validEnvironments) . '"';
64-
throw new \UnexpectedValueException(
67+
throw new UnexpectedValueException(
6568
'$environment must be one of ' . $environmentsText . ', but actually was: "' . $environment . '"',
6669
1499112172
6770
);

0 commit comments

Comments
 (0)