Skip to content

Commit

Permalink
Merge pull request #7 from fmasa/php71
Browse files Browse the repository at this point in the history
Add PHP 7.1 typehints
  • Loading branch information
fmasa authored Dec 25, 2017
2 parents f738392 + c377483 commit df445de
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 61 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ cache:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2

matrix:
include:
- php: 5.6
- php: 7.1
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-stable"
- php: 7.0
- php: 7.1
env: COVERAGE="--coverage-clover build/logs/clover.xml"
- php: 7.2
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fmasa/auto-di",
"type": "library",
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.1",
"nette/di": "^2.3",
"nette/robot-loader": "^2.4.2 || ^3.0"
},
Expand Down
28 changes: 8 additions & 20 deletions src/AutoDI/ClassList.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Fmasa\AutoDI;

class ClassList
Expand All @@ -16,22 +18,17 @@ public function __construct(array $classes)
$this->classes = array_values($classes);
}

/**
* @param string $classPattern
* @return ClassList
*/
public function getMatching($classPattern)
public function getMatching(string $classPattern): ClassList
{
$classes = preg_grep($this->buildRegex($classPattern), $this->classes);

return new ClassList($classes);
}

/**
* @param string $classPattern
* @return string
*/
private function buildRegex($classPattern)
private function buildRegex(string $classPattern): string
{
$replacements = [
'~\\*\\*~' => '(.*)', // ** for n-level wildcard
Expand All @@ -50,10 +47,7 @@ private function buildRegex($classPattern)
return "~^$regex$~";
}

/**
* @return ClassList
*/
public function getClasses()
public function getClasses(): ClassList
{
$classes = array_filter($this->classes, function ($c) {
$reflection = new \ReflectionClass($c);
Expand All @@ -63,10 +57,7 @@ public function getClasses()
return new ClassList($classes);
}

/**
* @return ClassList
*/
public function getInterfaces()
public function getInterfaces(): ClassList
{
$interfaces = array_filter($this->classes, function ($c) {
return (new \ReflectionClass($c))->isInterface();
Expand All @@ -75,18 +66,15 @@ public function getInterfaces()
return new ClassList($interfaces);
}

/**
* @return ClassList
*/
public function getWithoutClasses(ClassList $list)
public function getWithoutClasses(ClassList $list): ClassList
{
return new ClassList(array_diff($this->classes, $list->classes));
}

/**
* @return string[]
*/
public function toArray()
public function toArray(): array
{
return $this->classes;
}
Expand Down
20 changes: 9 additions & 11 deletions src/AutoDI/DI/AutoDIExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Fmasa\AutoDI\DI;

use Fmasa\AutoDI\ClassList;
Expand All @@ -19,29 +21,26 @@ class AutoDIExtension extends CompilerExtension
'tempDir' => '%tempDir%',
];

public function beforeCompile()
public function beforeCompile(): void
{
if ( ! $this->shouldRegisterOnConfiguration()) {
$this->registerServices();
}
}

public function loadConfiguration()
public function loadConfiguration(): void
{
if ($this->shouldRegisterOnConfiguration()) {
$this->registerServices();
}
}

/**
* @return bool
*/
private function shouldRegisterOnConfiguration()
private function shouldRegisterOnConfiguration(): bool
{
return (bool) $this->getConfig($this->defaults)['registerOnConfiguration'];
}

private function registerServices()
private function registerServices(): void
{
$config = $this->getConfig($this->defaults);

Expand All @@ -62,7 +61,7 @@ private function registerServices()

foreach ($config['services'] as $service) {

list($field, $matchingClasses) = $this->getClasses($service, $classes);
[$field, $matchingClasses] = $this->getClasses($service, $classes);

if (isset($service['exclude'])) {
$excluded = $service['exclude'];
Expand Down Expand Up @@ -93,7 +92,7 @@ private function registerServices()
* @param ClassList $classes
* @return array [definition field, Class list]
*/
private function getClasses(array $service, ClassList $classes)
private function getClasses(array $service, ClassList $classes): array
{
$types = [
'class' => $classes->getClasses(),
Expand Down Expand Up @@ -126,9 +125,8 @@ private function getClasses(array $service, ClassList $classes)

/**
* @param string[] $exludedPatterns
* @return ClassList
*/
private function removeExcludedClasses(ClassList $classes, array $exludedPatterns)
private function removeExcludedClasses(ClassList $classes, array $exludedPatterns): ClassList
{
return array_reduce($exludedPatterns, function(ClassList $c, $pattern) {
return $c->getWithoutClasses($c->getMatching($pattern));
Expand Down
14 changes: 7 additions & 7 deletions tests/ClassListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ClassListTest extends TestCase
Tests\Dir01\SimpleService\AnotherService::class,
];

public function testClassFilterWithDirectoryWildcard()
public function testClassFilterWithDirectoryWildcard(): void
{
$filter = new ClassList(self::CLASSES);

Expand All @@ -30,7 +30,7 @@ public function testClassFilterWithDirectoryWildcard()
);
}

public function testClassFilterWithDirectoryWildcardWithClassName()
public function testClassFilterWithDirectoryWildcardWithClassName(): void
{
$filter = new ClassList(self::CLASSES);

Expand All @@ -42,7 +42,7 @@ public function testClassFilterWithDirectoryWildcardWithClassName()
);
}

public function testOneLevelWildcardForClassName()
public function testOneLevelWildcardForClassName(): void
{
$filter = new ClassList(self::CLASSES);

Expand All @@ -57,7 +57,7 @@ public function testOneLevelWildcardForClassName()
);
}

public function testGroupMatch()
public function testGroupMatch(): void
{
$classes = new ClassList(
array_merge(self::CLASSES, [Tests\Dir03\ForeignService::class])
Expand All @@ -68,7 +68,7 @@ public function testGroupMatch()
$this->assertSame(self::CLASSES, $matching->toArray());
}

public function testFilterClasses()
public function testFilterClasses(): void
{
$list = new ClassList([
Tests\Dir01\SimpleService::class,
Expand All @@ -86,7 +86,7 @@ public function testFilterClasses()
);
}

public function testGetClassesFiltersOutAbstractClasses()
public function testGetClassesFiltersOutAbstractClasses(): void
{
$list = new ClassList([
Tests\Dir01\SimpleService::class,
Expand All @@ -103,7 +103,7 @@ public function testGetClassesFiltersOutAbstractClasses()
);
}

public function testFilterInterfaces()
public function testFilterInterfaces(): void
{
$list = new ClassList([
Tests\Dir01\SimpleService::class,
Expand Down
33 changes: 15 additions & 18 deletions tests/DI/AutoDIExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Fmasa\AutoDI\DI;

use Nette\Configurator;
Expand All @@ -10,35 +12,35 @@
class AutoDIExtensionTest extends TestCase
{

public function testOverrideDirectories()
public function testOverrideDirectories(): void
{
$container = $this->getContainer(__DIR__ . '/alternativeDirectories.neon');

$container->getByType(Tests\Dir01\AlternativeService::class);
$container->getByType(Tests\Dir02\AlternativeService::class);
}

public function testLoadByClassName()
public function testLoadByClassName(): void
{
$container = $this->getContainer(__DIR__ . '/className.neon');
$container->getByType(Tests\Dir01\SimpleService::class);
}

public function testLoadByClassNameWithDirectoryWildcard()
public function testLoadByClassNameWithDirectoryWildcard(): void
{
$container = $this->getContainer(__DIR__ . '/directoryWildcard.neon');
$container->getByType(Tests\Dir01\SimpleService::class);
$container->getByType(Tests\Dir02\SimpleService::class);
}

public function testSetTags()
public function testSetTags(): void
{
$container = $this->getContainer(__DIR__ . '/tags.neon');

$this->assertCount(1, $container->findByTag('test'));
}

public function testGeneratedFactory()
public function testGeneratedFactory(): void
{
$container = $this->getContainer(__DIR__ . '/generatedFactory.neon');

Expand All @@ -47,15 +49,15 @@ public function testGeneratedFactory()
$this->assertInstanceOf(Tests\Dir01\SimpleService::class, $factory->create());
}

public function testAlreadyRegisteredClassOrInterfaceIsNotRegistered()
public function testAlreadyRegisteredClassOrInterfaceIsNotRegistered(): void
{
$container = $this->getContainer(__DIR__ . '/alreadyRegistered.neon');

$this->assertCount(1, $container->findByType(Tests\Dir01\ISimpleServiceFactory::class));
$this->assertCount(1, $container->findByType(Tests\Dir01\SimpleService2::class));
}

public function testDefaultsAreUsedIfNotOverriden()
public function testDefaultsAreUsedIfNotOverriden(): void
{
$container = $this->getContainer(__DIR__ . '/defaults.neon');

Expand All @@ -64,7 +66,7 @@ public function testDefaultsAreUsedIfNotOverriden()
$this->assertCount(1, $services);
}

public function testDefaultsAreOverriden()
public function testDefaultsAreOverriden(): void
{
$container = $this->getContainer(__DIR__ . '/defaultsOverriden.neon');
$services = $container->findByTag('new');
Expand All @@ -73,7 +75,7 @@ public function testDefaultsAreOverriden()
$this->assertCount(1, $services);
}

public function testExcludePattern()
public function testExcludePattern(): void
{
$container = $this->getContainer(__DIR__ . '/excludePattern.neon');

Expand All @@ -82,7 +84,7 @@ public function testExcludePattern()
$this->assertNull($container->getByType(Tests\Dir02\SimpleService::class, false));
}

public function testExcludePatternList()
public function testExcludePatternList(): void
{
$container = $this->getContainer(__DIR__ . '/excludePatternList.neon');

Expand All @@ -96,7 +98,7 @@ public function testExcludePatternList()
* and second with registration on configuration. When registering same service by both,
* only second extension should register it
*/
public function testRegisterOnConfiguration()
public function testRegisterOnConfiguration(): void
{
$container = $this->getContainer(__DIR__ . '/onConfiguration.neon');

Expand All @@ -106,19 +108,14 @@ public function testRegisterOnConfiguration()
$this->assertCount(1, $container->findByType(Tests\Dir02\SimpleService::class));
}

public function testWorksWithNetteDIDecorator()
public function testWorksWithNetteDIDecorator(): void
{
$container = $this->getContainer(__DIR__ . '/decorator.neon');

$this->assertCount(1, $container->findByTag('decorated'));
}

/**
* @param string $configFile
* @param string $appDir
* @return Container
*/
private function getContainer($configFile, $appDir = __DIR__ . '/../fixtures/app')
private function getContainer(string $configFile, string $appDir = __DIR__ . '/../fixtures/app'): Container
{
$configurator = new Configurator();
$configurator->setTempDirectory(__DIR__ . '/../temp');
Expand Down

0 comments on commit df445de

Please sign in to comment.