Skip to content

Commit

Permalink
Improve coding style to extended PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
bnf committed Sep 12, 2018
1 parent 4c6d475 commit 5e3ce28
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
composer.lock
build/
.php_cs.cache
74 changes: 74 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* This file represents the configuration for Code Sniffing PSR-2-related
* automatic checks of coding guidelines
* Install @fabpot's great php-cs-fixer tool via
*
* $ composer global require friendsofphp/php-cs-fixer
*
* And then simply run
*
* $ php-cs-fixer fix --config ../Build/.php_cs
*
* inside the TYPO3 directory. Warning: This may take up to 10 minutes.
*
* For more information read:
* https://www.php-fig.org/psr/psr-2/
* https://cs.sensiolabs.org
*/
if (PHP_SAPI !== 'cli') {
die('This script supports command line usage only. Please check your command.');
}
// Define in which folders to search and which folders to exclude
// Exclude some directories that are excluded by Git anyways to speed up the sniffing
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__ . '/');
// Return a Code Sniffing configuration using
// all sniffers needed for PSR-2
// and additionally:
// - Remove leading slashes in use clauses.
// - PHP single-line arrays should not have trailing comma.
// - Single-line whitespace before closing semicolon are prohibited.
// - Remove unused use statements in the PHP source code
// - Ensure Concatenation to have at least one whitespace around
// - Remove trailing whitespace at the end of blank lines.
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'no_leading_import_slash' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_unused_imports' => true,
'concat_space' => ['spacing' => 'one'],
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'single_quote' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'no_blank_lines_after_phpdoc' => true,
'array_syntax' => ['syntax' => 'short'],
'whitespace_after_comma_in_array' => true,
'function_typehint_space' => true,
'hash_to_slash_comment' => true,
'no_alias_functions' => true,
'lowercase_cast' => true,
'no_leading_namespace_whitespace' => true,
'native_function_casing' => true,
'no_short_bool_cast' => true,
'no_unneeded_control_parentheses' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_trim' => true,
'no_superfluous_elseif' => true,
'no_useless_else' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'return_type_declaration' => ['space_before' => 'none'],
'cast_spaces' => ['space' => 'none'],
'declare_equal_normalize' => ['space' => 'single'],
'dir_constant' => true,
])
->setFinder($finder);
2 changes: 1 addition & 1 deletion src/InteropServiceProviderBridgeBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function build(ContainerBuilder $container)
*/
public function boot()
{
$registryServiceName = 'service_provider_registry_'.$this->id;
$registryServiceName = 'service_provider_registry_' . $this->id;
$this->container->set($registryServiceName, $this->getRegistry($this->container));
}

Expand Down
41 changes: 20 additions & 21 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Bnf\Interop\ServiceProviderBridgeBundle;

use Psr\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

/**
* A class that holds the list of service providers of a project.
Expand Down Expand Up @@ -111,28 +111,27 @@ public function offsetGet($offset)
{
if (isset($this->constructedArray[$offset])) {
return $this->constructedArray[$offset];
} else {
$item = $this->lazyArray[$offset];
if ($item instanceof ServiceProviderInterface) {
$this->constructedArray[$offset] = $item;
}
$item = $this->lazyArray[$offset];
if ($item instanceof ServiceProviderInterface) {
$this->constructedArray[$offset] = $item;

return $item;
}
return $item;
}

if (is_array($item)) {
$className = $item[0];
$params = isset($item[1]) ? $item[1] : [];
} elseif (is_string($item)) {
$className = $item;
$params = [];
} else {
throw new \InvalidArgumentException('lazyArray elements are expected to be a fully qualified class name or an instance of Interop\\Container\\ServiceProviderInterface');
}
if (is_array($item)) {
$className = $item[0];
$params = isset($item[1]) ? $item[1] : [];
} elseif (is_string($item)) {
$className = $item;
$params = [];
} else {
throw new \InvalidArgumentException('lazyArray elements are expected to be a fully qualified class name or an instance of Interop\\Container\\ServiceProviderInterface');
}

$this->constructedArray[$offset] = new $className(...$params);
$this->constructedArray[$offset] = new $className(...$params);

return $this->constructedArray[$offset];
}
return $this->constructedArray[$offset];
}

/**
Expand Down Expand Up @@ -178,7 +177,7 @@ public function offsetUnset($offset)
*
* @return array
*/
public function getFactories($offset) : array
public function getFactories($offset): array
{
if (!isset($this->serviceFactories[$offset])) {
$this->serviceFactories[$offset] = $this->offsetGet($offset)->getFactories();
Expand All @@ -195,7 +194,7 @@ public function getFactories($offset) : array
*
* @return array
*/
public function getExtensions($offset) : array
public function getExtensions($offset): array
{
if (!isset($this->serviceExtensions[$offset])) {
$this->serviceExtensions[$offset] = $this->offsetGet($offset)->getExtensions();
Expand Down
29 changes: 15 additions & 14 deletions src/ServiceProviderCompilationPass.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php


namespace Bnf\Interop\ServiceProviderBridgeBundle;


use Interop\Container\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -59,48 +56,52 @@ public function process(ContainerBuilder $container)
}
}


private function registerRegistry(ContainerBuilder $container)
{
$definition = new Definition(Registry::class);
$definition->setSynthetic(true);
$definition->setPublic(true);

$container->setDefinition('service_provider_registry_'.$this->registryId, $definition);
$container->setDefinition('service_provider_registry_' . $this->registryId, $definition);
}

private function registerFactories($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) {
private function registerFactories($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container)
{
$serviceFactories = $serviceProvider->getFactories();

foreach ($serviceFactories as $serviceName => $callable) {
$this->registerService($serviceName, $serviceProviderKey, $callable, $container);
}
}

private function registerExtensions($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container) {
private function registerExtensions($serviceProviderKey, ServiceProviderInterface $serviceProvider, ContainerBuilder $container)
{
$serviceFactories = $serviceProvider->getExtensions();

foreach ($serviceFactories as $serviceName => $callable) {
$this->extendService($serviceName, $serviceProviderKey, $callable, $container);
}
}

private function registerService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) {
private function registerService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container)
{
$this->addServiceDefinitionFromCallable($serviceName, $serviceProviderKey, $callable, $container);
}

private function extendService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container) {
private function extendService($serviceName, $serviceProviderKey, $callable, ContainerBuilder $container)
{
$this->addServiceDefinitionFromCallable($serviceName, $serviceProviderKey, $callable, $container, 'extendService');
}

private function getDecoratedServiceName($serviceName, ContainerBuilder $container) {
private function getDecoratedServiceName($serviceName, ContainerBuilder $container)
{
$counter = 1;
while ($container->has($serviceName.'_decorated_'.$counter)) {
while ($container->has($serviceName . '_decorated_' . $counter)) {
$counter++;
}
return [
$serviceName.'_decorated_'.$counter,
$counter === 1 ? $serviceName : $serviceName.'_decorated_'.($counter-1)
$serviceName . '_decorated_' . $counter,
$counter === 1 ? $serviceName : $serviceName . '_decorated_' . ($counter-1)
];
}

Expand All @@ -122,7 +123,7 @@ private function addServiceDefinitionFromCallable($serviceName, $serviceProvider
if ($this->isStaticallyCallable($callable)) {
$factoryDefinition->setFactory(/** @scrutinizer ignore-type */$callable);
} else {
$factoryDefinition->setFactory([ new Reference('service_provider_registry_'.$this->registryId), $method ]);
$factoryDefinition->setFactory([ new Reference('service_provider_registry_' . $this->registryId), $method ]);
$factoryDefinition->addArgument($serviceProviderKey);
$factoryDefinition->addArgument($serviceName);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Fixtures/TestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

function myFunctionFactory()
{
Expand All @@ -25,7 +25,8 @@ public function getFactories()
return new \stdClass();
},
'serviceD' => new class {
public function __invoke(ContainerInterface $container): \stdClass {
public function __invoke(ContainerInterface $container): \stdClass
{
return new \stdClass();
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestServiceProviderOverride.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

class TestServiceProviderOverride implements ServiceProviderInterface
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/TestServiceProviderOverride2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

class TestServiceProviderOverride2 implements ServiceProviderInterface
{
Expand All @@ -23,7 +23,7 @@ public function getExtensions()
{
return [
'serviceA' => [self::class, 'overrideServiceA'],
'serviceC' => function(ContainerInterface $container, \stdClass $instance): \stdClass {
'serviceC' => function (ContainerInterface $container, \stdClass $instance): \stdClass {
$instance->serviceB = $container->get('serviceB');

return $instance;
Expand Down
12 changes: 4 additions & 8 deletions tests/ServiceProviderCompilationPassTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php


namespace Bnf\Interop\ServiceProviderBridgeBundle\Tests;


use Bnf\Interop\ServiceProviderBridgeBundle\InteropServiceProviderBridgeBundle;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProvider;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride2;
use Interop\Container\ServiceProviderInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Bnf\Interop\ServiceProviderBridgeBundle\InteropServiceProviderBridgeBundle;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProvider;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride2;

class ServiceProviderCompilationPassTest extends TestCase
{
Expand Down Expand Up @@ -81,7 +78,6 @@ public function getExtensions()
{
return [];
}

}
]);
$container = new ContainerBuilder();
Expand Down
4 changes: 2 additions & 2 deletions tests/ServiceProviderRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Bnf\Interop\ServiceProviderBridgeBundle\Tests;

use PHPUnit\Framework\TestCase;
use Bnf\Di\Container;
use Bnf\Interop\ServiceProviderBridgeBundle\Registry;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestRegistryServiceProvider;
use Bnf\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestStatefulServiceProvider;
use PHPUnit\Framework\TestCase;

class ServiceProviderRegistryTest extends TestCase
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public function testPushException()
{
$registry = new Registry();

$registry->push(array());
$registry->push([]);
}

/**
Expand Down

0 comments on commit 5e3ce28

Please sign in to comment.