Skip to content

Commit 652d612

Browse files
committed
introduce phpstan
1 parent 045bd50 commit 652d612

File tree

6 files changed

+225
-44
lines changed

6 files changed

+225
-44
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"symfony/browser-kit": "^6.4",
2828
"symfony/framework-bundle": "^6.4",
2929
"symfony/http-kernel": "^6.4",
30-
"symfony/stopwatch": "^6.4"
30+
"symfony/stopwatch": "^6.4",
31+
"phpstan/phpstan": "^1.11",
32+
"phpstan/phpstan-symfony": "^1.4"
3133
},
3234
"autoload": {
3335
"psr-4": {
@@ -43,6 +45,7 @@
4345
"ci": [
4446
"composer validate",
4547
"@check-cs",
48+
"@phpstan",
4649
"@phpunit"
4750
],
4851
"check-cs": [
@@ -53,6 +56,9 @@
5356
],
5457
"phpunit": [
5558
"phpunit"
59+
],
60+
"phpstan": [
61+
"phpstan analyse -c phpstan.neon"
5662
]
5763
},
5864
"config": {

composer.lock

Lines changed: 131 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
includes:
2+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
3+
- vendor/phpstan/phpstan-symfony/extension.neon
4+
- vendor/phpstan/phpstan-symfony/rules.neon
5+
6+
parameters:
7+
phpVersion: 80100
8+
level: max
9+
paths:
10+
- src
11+
- tests
12+
symfony:
13+
containerXmlPath: var/cache/test/Chaos_Monkey_Symfony_Tests_Symfony_KernelTestDebugContainer.xml

src/DependencyInjection/ChaosMonkeyExtension.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,42 @@
99
use Symfony\Component\DependencyInjection\Extension\Extension;
1010
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1111

12+
/**
13+
* @phpstan-type ConfigArray array{
14+
* enabled: bool,
15+
* probability: int,
16+
* assaults: array{
17+
* latency: array{active: bool, minimum: int, maximum: int},
18+
* memory: array{active: bool, fill_fraction: float},
19+
* exception: array{active: bool, class: class-string<\Throwable>},
20+
* kill_app: array{active: bool}
21+
* },
22+
* watchers: array{
23+
* request: array{enabled: bool, priority: int}
24+
* }
25+
* }
26+
*/
1227
class ChaosMonkeyExtension extends Extension
1328
{
14-
public function load(array $configs, ContainerBuilder $container)
29+
/**
30+
* @param array<array<mixed>> $configs
31+
*/
32+
public function load(array $configs, ContainerBuilder $container): void
1533
{
1634
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1735
$loader->load('services.xml');
1836

1937
$configuration = new Configuration();
38+
/** @var ConfigArray $config */
2039
$config = $this->processConfiguration($configuration, $configs);
2140

2241
$this->setChaosMonkeySettings($container, $config);
2342
$this->enableWatchers($container, $config);
2443
}
2544

45+
/**
46+
* @param ConfigArray $config
47+
*/
2648
private function setChaosMonkeySettings(ContainerBuilder $container, array $config): void
2749
{
2850
$definition = $container->getDefinition('chaos_monkey.settings');
@@ -42,6 +64,9 @@ private function setChaosMonkeySettings(ContainerBuilder $container, array $conf
4264
$definition->addMethodCall('setKillAppActive', [$config['assaults']['kill_app']['active']]);
4365
}
4466

67+
/**
68+
* @param ConfigArray $config
69+
*/
4570
private function enableWatchers(ContainerBuilder $container, array $config): void
4671
{
4772
if ($config['watchers']['request']['enabled'] === true) {

tests/Controller/SymfonyControllerTest.php

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@
55
namespace Chaos\Monkey\Symfony\Tests\Controller;
66

77
use Chaos\Monkey\Settings;
8-
use Chaos\Monkey\Symfony\ChaosMonkeyBundle;
8+
use Chaos\Monkey\Symfony\Tests\Symfony\Kernel;
99
use PHPUnit\Framework\TestCase;
10-
use Psr\Log\NullLogger;
11-
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
12-
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1310
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
14-
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
15-
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\HttpKernel\Kernel;
17-
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1811
use Symfony\Component\Stopwatch\Stopwatch;
1912

2013
class SymfonyControllerTest extends TestCase
@@ -23,7 +16,7 @@ class SymfonyControllerTest extends TestCase
2316

2417
protected function setUp(): void
2518
{
26-
$this->client = new KernelBrowser($kernel = new SymfonyControllerKernel('test', false));
19+
$this->client = new KernelBrowser($kernel = new Kernel('test', true));
2720
$this->client->disableReboot();
2821
$kernel->boot();
2922
}
@@ -92,35 +85,3 @@ private function chaosMonkeySettings(): Settings
9285
return $this->client->getContainer()->get('chaos_monkey')->settings();
9386
}
9487
}
95-
96-
class SymfonyControllerKernel extends Kernel
97-
{
98-
use MicroKernelTrait;
99-
100-
public function registerBundles(): iterable
101-
{
102-
return [
103-
new FrameworkBundle(),
104-
new ChaosMonkeyBundle(),
105-
];
106-
}
107-
108-
protected function configureContainer(ContainerConfigurator $c): void
109-
{
110-
$c->extension('framework', [
111-
'secret' => 'S0ME_SECRET',
112-
]);
113-
114-
$c->services()->set('logger', NullLogger::class);
115-
}
116-
117-
protected function configureRoutes(RoutingConfigurator $routes)
118-
{
119-
$routes->add('home', '/hello')->controller([$this, 'hello']);
120-
}
121-
122-
public function hello(): Response
123-
{
124-
return new Response('Hello world');
125-
}
126-
}

tests/Symfony/Kernel.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chaos\Monkey\Symfony\Tests\Symfony;
6+
7+
use Chaos\Monkey\Symfony\ChaosMonkeyBundle;
8+
use Psr\Log\NullLogger;
9+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
10+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
11+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
12+
use Symfony\Component\HttpFoundation\Response;
13+
use Symfony\Component\HttpKernel\Kernel as SymfonyKernel;
14+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
15+
16+
final class Kernel extends SymfonyKernel
17+
{
18+
use MicroKernelTrait;
19+
20+
public function registerBundles(): iterable
21+
{
22+
return [
23+
new FrameworkBundle(),
24+
new ChaosMonkeyBundle(),
25+
];
26+
}
27+
28+
protected function configureContainer(ContainerConfigurator $container): void
29+
{
30+
$container->extension('framework', [
31+
'secret' => 'S0ME_SECRET',
32+
]);
33+
34+
$container->services()->set('logger', NullLogger::class);
35+
}
36+
37+
protected function configureRoutes(RoutingConfigurator $routes): void
38+
{
39+
$routes->add('home', '/hello')->controller([$this, 'hello']);
40+
}
41+
42+
public function hello(): Response
43+
{
44+
return new Response('Hello world');
45+
}
46+
}

0 commit comments

Comments
 (0)