Skip to content

Commit

Permalink
Merge pull request #42 from lcobucci/improve-type-declaration
Browse files Browse the repository at this point in the history
Improve type declaration
  • Loading branch information
lcobucci authored Feb 26, 2020
2 parents d7d3f97 + 9af85ee commit d289fa7
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for mutation"; exit 1; fi
script:
- ./vendor/bin/infection -s --min-msi=98 --min-covered-msi=98 --threads=$(nproc)
- ./vendor/bin/infection -s --min-msi=100 --min-covered-msi=100 --threads=$(nproc)

- stage: Quality
env: STATIC_ANALYSIS=1
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"infection/infection": "^0.15",
"lcobucci/coding-standard": "^4.0",
"mikey179/vfsstream": "^1.6.8",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
Expand Down
7 changes: 0 additions & 7 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon

parameters:
level: 7
paths:
- src
- test
checkMissingIterableValueType: false
5 changes: 4 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function class_exists;
use function dirname;
use function is_array;
use function is_int;
use function is_string;

final class Compiler
Expand Down Expand Up @@ -55,13 +56,15 @@ private function configurePassList(
ContainerConfiguration $config
): void {
foreach ($config->getPassList() as $passConfig) {
assert(is_array($passConfig));
[$pass, $type, $priority] = $passConfig + self::DEFAULT_PASS_CONFIG;
assert(is_string($type));
assert(is_int($priority));

if (! $pass instanceof CompilerPassInterface) {
[$className, $constructArguments] = $pass;

$pass = new $className(...$constructArguments);
assert($pass instanceof CompilerPassInterface);
}

$container->addCompilerPass($pass, $type, $priority);
Expand Down
4 changes: 4 additions & 0 deletions src/CompilerPassListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

use Generator as DefaultGenerator;
use Lcobucci\DependencyInjection\Config\Package;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

interface CompilerPassListProvider extends Package
{
/**
* @return DefaultGenerator<array<CompilerPassInterface|string|int|array<string|array<mixed>>>>
*/
public function getCompilerPasses(): DefaultGenerator;
}
8 changes: 7 additions & 1 deletion src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ContainerConfiguration
private array $files;

/**
* @var mixed[]
* @var array<int, array<int, CompilerPassInterface|string|int|array<string|array<mixed>>>>
*/
private array $passList;

Expand Down Expand Up @@ -97,6 +97,9 @@ public function addPackage(string $className, array $constructArguments = []): v
$this->packages[] = [$className, $constructArguments];
}

/**
* @return Generator<string>
*/
public function getFiles(): Generator
{
foreach ($this->filterPackages(FileListProvider::class) as $package) {
Expand Down Expand Up @@ -126,6 +129,9 @@ public function addFile(string $file): void
$this->files[] = $file;
}

/**
* @return Generator<array<CompilerPassInterface|string|int|array<int, string|array<mixed>>>>
*/
public function getPassList(): Generator
{
foreach ($this->filterPackages(CompilerPassListProvider::class) as $package) {
Expand Down
3 changes: 3 additions & 0 deletions src/FileListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

interface FileListProvider extends Package
{
/**
* @return DefaultGenerator<string>
*/
public function getFiles(): DefaultGenerator;
}
12 changes: 7 additions & 5 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use function assert;
use function count;
use function file_put_contents;
use function iterator_to_array;
Expand Down Expand Up @@ -86,8 +85,6 @@ public function compileShouldCreateMultipleFiles(): void
self::assertCount(count($expectedFiles), $generatedFiles);

foreach ($generatedFiles as $name => $file) {
assert($file instanceof vfsStreamFile);

self::assertContains($name, $expectedFiles);
self::assertSame($expectedPermissions, $file->getPermissions());
}
Expand Down Expand Up @@ -141,6 +138,9 @@ public function compilationShouldBeSkippedWhenFileAlreadyExists(): void
self::assertCount(1, $generatedFiles);
}

/**
* @return PHPGenerator<string, vfsStreamFile>
*/
private function getGeneratedFiles(vfsStreamDirectory $directory): PHPGenerator
{
foreach ($directory->getChildren() as $child) {
Expand All @@ -150,9 +150,11 @@ private function getGeneratedFiles(vfsStreamDirectory $directory): PHPGenerator
continue;
}

if ($child->getName() !== 'services.yml') {
yield $child->getName() => $child;
if (! $child instanceof vfsStreamFile || $child->getName() === 'services.yml') {
continue;
}

yield $child->getName() => $child;
}
}
}
6 changes: 6 additions & 0 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function getFilesShouldYieldTheFilesFromPackagesFirst(): void
{
$package1 = new class implements CompilerPassListProvider
{
/**
* @return Generator<array<CompilerPassInterface|string|int|array<string|array<mixed>>>>
*/
public function getCompilerPasses(): Generator
{
yield [CompilerPassInterface::class, 'beforeOptimization'];
Expand All @@ -70,6 +73,9 @@ public function getCompilerPasses(): Generator

$package2 = new class implements FileListProvider
{
/**
* @return Generator<string>
*/
public function getFiles(): Generator
{
yield 'services2.xml';
Expand Down
9 changes: 7 additions & 2 deletions test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use function get_class;
use function iterator_to_array;

final class ContainerBuilderTest extends TestCase
{
Expand Down Expand Up @@ -278,7 +279,7 @@ public function getContainerShouldGenerateAndReturnTheContainer(): void

$this->generator->expects(self::once())
->method('generate')
->with($this->config, self::equalTo(new ConfigCache($this->config->getDumpFile(), false)))
->with($this->config, new ConfigCache($this->config->getDumpFile(), false))
->willReturn($container);

self::assertSame($container, $builder->getContainer());
Expand Down Expand Up @@ -307,9 +308,13 @@ public function getTestContainerShouldGenerateAndReturnTheContainer(): void

$this->generator->expects(self::once())
->method('generate')
->with(self::equalTo($config), self::equalTo($cacheConfig))
->with($config, $cacheConfig)
->willReturn($container);

self::assertSame($container, $builder->getTestContainer());

$compilerPasses = iterator_to_array($this->config->getPassList());
self::assertCount(1, $compilerPasses);
self::assertSame($this->parameterBag, $compilerPasses[0][0]);
}
}

0 comments on commit d289fa7

Please sign in to comment.