Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Refactor code (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 29, 2021
1 parent 09e172b commit eb3e246
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 59 deletions.
6 changes: 2 additions & 4 deletions src/Command/PsyshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
*/
final class PsyshCommand extends Command
{
private $psysh;
private Application $psysh;

public function __construct(Application $psysh)
{
parent::__construct();

$this->psysh = $psysh;

return 0;
}

protected function configure(): void
Expand All @@ -40,6 +38,6 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
return (int) $this->psysh->run($input, $output);
return $this->psysh->run($input, $output);
}
}
3 changes: 1 addition & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ final class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('psysh');
$rootNode = $treeBuilder->getRootNode();

$rootNode
$treeBuilder->getRootNode()
->children()
->arrayNode('variables')
->info('Define additional variables to be exposed in Psysh')
Expand Down
9 changes: 7 additions & 2 deletions src/DependencyInjection/PsyshExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ final class PsyshExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(self::CONFIG_DIR));
$loader = new Loader\XmlFileLoader(
$container,
new FileLocator(self::CONFIG_DIR),
);
$loader->load('services.xml');

$config = $this->processConfiguration(new Configuration(), $configs);

foreach ($config['variables'] as $name => &$value) {
foreach ($config['variables'] as $name => $value) {
if (is_string($value) && strpos($value, '@') === 0) {
$value = new Reference(substr($value, 1));
}

$config['variables'][$name] = $value;
}

$containerId = 'test.service_container';
Expand Down
2 changes: 1 addition & 1 deletion src/PsyshBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(
new AddPsyshCommandPass(),
PassConfig::TYPE_BEFORE_OPTIMIZATION,
10
10,
);
}
}
16 changes: 5 additions & 11 deletions src/PsyshFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,24 @@
*/
final class PsyshFacade implements ContainerAwareInterface
{
/**
* @var Shell
*/
private static $shell;
private static Shell $shell;

/**
* @var ContainerInterface|null
*/
private static $container;
private static ContainerInterface $container;

public static function init(): void
{
if (null !== self::$shell) {
if (isset(self::$shell)) {
return;
}

if (null === self::$container) {
if (!isset(self::$container)) {
throw new RuntimeException('Cannot initialize the facade without a container.');
}

self::$shell = self::$container->get('psysh.shell');
}

public static function debug(array $variables = [], $bind = null)
public static function debug(array $variables = [], $bind = null): void
{
self::init();

Expand Down
18 changes: 2 additions & 16 deletions src/psysh.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,10 @@
* file that was distributed with this source code.
*/

namespace
{
if (false === function_exists('psysh')) {
function psysh(array $variables = [], $bind = null)
{
@trigger_error(
'This method is deprecated since 3.2.0. Use Fidry\PsyshBundle\psysh instead.',
E_USER_DEPRECATED
);
\Fidry\PsyshBundle\PsyshFacade::debug($variables, $bind);
}
}
}

namespace Fidry\PsyshBundle
{
function psysh(array $variables = [], $bind = null)
function psysh(array $variables = [], $bind = null): void
{
\Fidry\PsyshBundle\PsyshFacade::debug($variables, $bind);
PsyshFacade::debug($variables, $bind);
}
}
17 changes: 6 additions & 11 deletions tests/Command/PsyshCommandIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
use Symfony\Component\HttpKernel\Kernel;
use function array_keys;
use function version_compare;

/**
* @author Théo FIDRY <theo.fidry@gmail.com>
Expand All @@ -24,23 +26,16 @@
*/
class PsyshCommandIntegrationTest extends KernelTestCase
{
/**
* @var Shell
*/
private $shell;
private Shell $shell;

/**
* @var PsyshCommand
*/
private $command;
private PsyshCommand $command;

protected function setUp(): void
{
self::bootKernel();

$container = version_compare(Kernel::VERSION, '5.3.0') >= 0 ? static::getContainer() : static::$container;
$this->shell = $container->get('psysh.shell');
$this->command = $container->get('psysh.command.shell_command');
$this->shell = static::getContainer()->get('psysh.shell');
$this->command = static::getContainer()->get('psysh.command.shell_command');
}

public function testScopeVariables(): void
Expand Down
11 changes: 1 addition & 10 deletions tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ public function registerBundles(): array

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__.'/config.yml');
}

protected function configureContainer(ContainerConfigurator $container, LoaderInterface $loader)
{
$params = version_compare(Kernel::VERSION, '5.3.0') >= 0
? ["session" => ['storage_factory_id' => 'session.storage.factory.mock_file']]
: ["session" => ['storage_id' => 'session.storage.mock_file']];

$container->extension('framework', [$params]);
$loader->load(__DIR__.'/config.yaml');
}
}
2 changes: 2 additions & 0 deletions tests/Functional/config.yml → tests/Functional/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ framework:
resource: ~
strict_requirements: '%kernel.debug%'
test: ~
session:
storage_factory_id: 'session.storage.factory.mock_file'
6 changes: 4 additions & 2 deletions tests/PsyshBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function testServicesLoading(): void
{
static::bootKernel();

$container = version_compare(Kernel::VERSION, '5.3.0') >= 0 ? static::getContainer() : static::$container;
$this->assertInstanceOf(Shell::class, $container->get('psysh.shell'));
$this->assertInstanceOf(
Shell::class,
static::getContainer()->get('psysh.shell'),
);
}
}

0 comments on commit eb3e246

Please sign in to comment.