Skip to content
This repository has been archived by the owner on Apr 10, 2020. It is now read-only.

Commit

Permalink
added scalar type hints; minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guite committed Mar 26, 2019
1 parent 308189c commit 9d4b306
Show file tree
Hide file tree
Showing 33 changed files with 518 additions and 494 deletions.
4 changes: 3 additions & 1 deletion Application.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Zikula\Bundle\GeneratorBundle;

use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;
Expand All @@ -12,6 +14,6 @@ public function __construct(KernelInterface $kernel)
parent::__construct($kernel);

$this->setName('Zikula');
$this->setVersion('0.0.1 - '.$kernel->getName().'/'.$kernel->getEnvironment().($kernel->isDebug() ? '/debug' : ''));
$this->setVersion('0.0.1 - ' . $kernel->getEnvironment().($kernel->isDebug() ? '/debug' : ''));
}
}
41 changes: 22 additions & 19 deletions Command/GenerateBundleCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -11,11 +13,15 @@

namespace Zikula\Bundle\GeneratorBundle\Command;

use Exception;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Zikula\Bundle\GeneratorBundle\Generator\BundleGenerator;
use Zikula\Bundle\GeneratorBundle\Generator\Generator;

/**
* Generates bundles.
Expand Down Expand Up @@ -64,24 +70,22 @@ protected function configure()
/**
* @see Command
*
* @throws \InvalidArgumentException When namespace doesn't end with Bundle
* @throws \RuntimeException When bundle can't be executed
* @throws InvalidArgumentException When namespace doesn't end with Bundle
* @throws RuntimeException When bundle can't be executed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

if ($input->isInteractive()) {
if (!$io->confirm('Do you confirm generation?', true)) {
$io->error('Command aborted');
if ($input->isInteractive() && !$io->confirm('Do you confirm generation?', true)) {
$io->error('Command aborted');

return 1;
}
return 1;
}

foreach (['namespace', 'dir'] as $option) {
if (null === $input->getOption($option)) {
throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
throw new RuntimeException(sprintf('The "%s" option must be provided.', $option));
}
}

Expand Down Expand Up @@ -121,7 +125,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$namespace = null;
try {
$namespace = $input->getOption('namespace') ? Validators::validateBundleNamespace($input->getOption('namespace')) : null;
} catch (\Exception $error) {
} catch (Exception $error) {
$io->writeln($this->getHelper('formatter')->formatBlock($error->getMessage(), 'error'));
}

Expand All @@ -148,7 +152,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$bundle = null;
try {
$bundle = $input->getOption('module-name') ? Validators::validateBundleName($input->getOption('module-name')) : null;
} catch (\Exception $error) {
} catch (Exception $error) {
$io->writeln($this->getHelper('formatter')->formatBlock($error->getMessage(), 'error'));
}

Expand All @@ -172,7 +176,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$dir = null;
try {
$dir = $input->getOption('dir') ? Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace) : null;
} catch (\Exception $error) {
} catch (Exception $error) {
$io->writeln($this->getHelper('formatter')->formatBlock($error->getMessage(), 'error'));
}

Expand All @@ -192,7 +196,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$format = null;
try {
$format = $input->getOption('format') ? Validators::validateFormat($input->getOption('format')) : null;
} catch (\Exception $error) {
} catch (Exception $error) {
$io->writeln($this->getHelper('formatter')->formatBlock($error->getMessage(), 'error'));
}

Expand All @@ -203,22 +207,21 @@ protected function interact(InputInterface $input, OutputInterface $output)
'The default (if left blank) and recommended format is annotation.',
'',
]);
$defaultFormat = (null !== $input->getOption('format') ? $input->getOption('format') : 'annotation');
$defaultFormat = ($input->getOption('format') ?? 'annotation');
$format = $io->choice('Configuration format (yml, xml, php, or annotation)', ['yml', 'xml', 'php', 'annotation'], $defaultFormat);
$input->setOption('format', $format);
}

// summary
$io->block('Summary before generation', null, 'bg=blue;fg=white', ' ', true);
$io->writeln([
'',
sprintf("You are going to generate a \"<info>%s\\%s</info>\" module\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, $format),
''
]);
'',
sprintf("You are going to generate a \"<info>%s\\%s</info>\" module\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, $format),
''
]);
}


protected function createGenerator()
protected function createGenerator(): Generator
{
return new BundleGenerator($this->getContainer()->get('filesystem'));
}
Expand Down
54 changes: 29 additions & 25 deletions Command/GenerateControllerCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -11,12 +13,15 @@

namespace Zikula\Bundle\GeneratorBundle\Command;

use Exception;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Zikula\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Zikula\Bundle\GeneratorBundle\Generator\ControllerGenerator;
use Zikula\Bundle\GeneratorBundle\Generator\Generator;

/**
* Generates controllers.
Expand Down Expand Up @@ -78,16 +83,14 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

if ($input->isInteractive()) {
if (!$io->confirm('Do you confirm generation?', true)) {
$io->error('Command aborted');
if ($input->isInteractive() && !$io->confirm('Do you confirm generation?', true)) {
$io->error('Command aborted');

return 1;
}
return 1;
}

if (null === $input->getOption('controller')) {
throw new \RuntimeException('The controller option must be provided.');
throw new RuntimeException('The controller option must be provided.');
}

list($bundle, $controller) = $this->parseShortcutNotation($input->getOption('controller'));
Expand All @@ -96,7 +99,7 @@ public function execute(InputInterface $input, OutputInterface $output)

try {
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
} catch (\Exception $e) {
} catch (Exception $exception) {
$output->writeln(sprintf('<bg=red>Bundle "%s" does not exists.</>', $bundle));
}
}
Expand Down Expand Up @@ -127,6 +130,7 @@ public function interact(InputInterface $input, OutputInterface $output)
'',
]);

$bundle = $controller = '';
while (true) {
$controller = $io->ask('Controller name', $input->getOption('controller'), ['Zikula\Bundle\GeneratorBundle\Command\Validators', 'validateControllerName']);
list($bundle, $controller) = $this->parseShortcutNotation($controller);
Expand All @@ -139,7 +143,7 @@ public function interact(InputInterface $input, OutputInterface $output)
}

$io->error(sprintf('Controller "%s:%s" already exists.', $bundle, $controller));
} catch (\Exception $e) {
} catch (Exception $exception) {
$io->error(sprintf('Bundle "%s" does not exists.', $bundle));
}
}
Expand All @@ -151,7 +155,7 @@ public function interact(InputInterface $input, OutputInterface $output)
'Determine the format to use for the routing.',
'',
]);
$defaultFormat = (null !== $input->getOption('route-format') ? $input->getOption('route-format') : 'annotation');
$defaultFormat = ($input->getOption('route-format') ?? 'annotation');
$routeFormat = $io->choice('Routing format (yml, xml, php, or annotation)', ['yml', 'xml', 'php', 'annotation'], $defaultFormat);
$input->setOption('route-format', $routeFormat);

Expand All @@ -168,7 +172,7 @@ public function interact(InputInterface $input, OutputInterface $output)
]);
}

public function addActions(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
public function addActions(InputInterface $input, OutputInterface $output, SymfonyStyle $io): array
{
$output->writeln([
'',
Expand All @@ -179,12 +183,12 @@ public function addActions(InputInterface $input, OutputInterface $output, Symfo
]);

$templateNameValidator = function($name) {
if ('default' == $name) {
if ('default' === $name) {
return $name;
}

if (2 != substr_count($name, ':')) {
throw new \InvalidArgumentException(sprintf('Template name "%s" does not have 2 colons', $name));
if (2 !== substr_count($name, ':')) {
throw new InvalidArgumentException(sprintf('Template name "%s" does not have 2 colons', $name));
}

return $name;
Expand All @@ -196,16 +200,16 @@ public function addActions(InputInterface $input, OutputInterface $output, Symfo
// name
$output->writeln('');
$actionName = $io->ask('New action name (press <return> to stop adding actions)', null, function ($name) use ($actions) {
if (null == $name) {
if (null === $name) {
return $name;
}

if (isset($actions[$name])) {
throw new \InvalidArgumentException(sprintf('Action "%s" is already defined', $name));
throw new InvalidArgumentException(sprintf('Action "%s" is already defined', $name));
}

if ('Action' != substr($name, -6)) {
throw new \InvalidArgumentException(sprintf('Name "%s" is not suffixed by Action', $name));
if ('Action' !== substr($name, -6)) {
throw new InvalidArgumentException(sprintf('Name "%s" is not suffixed by Action', $name));
}

return $name;
Expand Down Expand Up @@ -234,7 +238,7 @@ public function addActions(InputInterface $input, OutputInterface $output, Symfo
return $actions;
}

public function parseActions($actions)
public function parseActions($actions): array
{
if (is_array($actions)) {
return $actions;
Expand All @@ -247,20 +251,20 @@ public function parseActions($actions)

// name
if (!isset($data[0])) {
throw new \InvalidArgumentException('An action must have a name');
throw new InvalidArgumentException('An action must have a name');
}
$name = array_shift($data);

// route
$route = (isset($data[0]) && '' != $data[0]) ? array_shift($data) : '/'.substr($name, 0, -6);
$route = (isset($data[0]) && '' !== $data[0]) ? array_shift($data) : '/' . substr($name, 0, -6);
if ($route) {
$placeholders = $this->getPlaceholdersFromRoute($route);
} else {
$placeholders = [];
}

// template
$template = (0 < count($data) && '' != $data[0]) ? implode(':', $data) : 'default';
$template = (0 < count($data) && '' !== $data[0]) ? implode(':', $data) : 'default';

$newActions[$name] = [
'name' => $name,
Expand All @@ -281,18 +285,18 @@ public function getPlaceholdersFromRoute($route)
return $placeholders;
}

public function parseShortcutNotation($shortcut)
public function parseShortcutNotation($shortcut): array
{
$entity = str_replace('/', '\\', $shortcut);

if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(sprintf('The controller name must contain a : ("%s" given, expecting something like AcmeBlogModule:Post)', $entity));
throw new InvalidArgumentException(sprintf('The controller name must contain a : ("%s" given, expecting something like AcmeBlogModule:Post)', $entity));
}

return [substr($entity, 0, $pos), substr($entity, $pos + 1)];
}

protected function createGenerator()
protected function createGenerator(): Generator
{
return new ControllerGenerator($this->getContainer()->get('filesystem'));
}
Expand Down
14 changes: 9 additions & 5 deletions Command/GenerateDoctrineCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -12,26 +14,28 @@
namespace Zikula\Bundle\GeneratorBundle\Command;

use Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
use InvalidArgumentException;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;

abstract class GenerateDoctrineCommand extends GeneratorCommand
{
public function isEnabled()
public function isEnabled(): bool
{
return class_exists('Doctrine\\Bundle\\DoctrineBundle\\DoctrineBundle');
return class_exists(DoctrineBundle::class);
}

protected function parseShortcutNotation($shortcut)
protected function parseShortcutNotation(string $shortcut): array
{
$entity = str_replace('/', '\\', $shortcut);

if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(sprintf('The entity name must contain a ":" ("%s" given, expecting something like AcmeBlogModule:Post)', $entity));
throw new InvalidArgumentException(sprintf('The entity name must contain a ":" ("%s" given, expecting something like AcmeBlogModule:Post)', $entity));
}

return [substr($entity, 0, $pos), substr($entity, $pos + 1)];
}

protected function getEntityMetadata($entity)
protected function getEntityMetadata(string $entity): array
{
$factory = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));

Expand Down
Loading

0 comments on commit 9d4b306

Please sign in to comment.