Skip to content

Commit

Permalink
Implemented PHPStan + Propel2 beta2 (#26)
Browse files Browse the repository at this point in the history
* composer require --dev phpstan/phpstan phpstan/extension-installer phpstan/phpstan-deprecation-rules

* Added phpstan.neon

* Fixed phpstan errors

* Bump to php 7.4 in preparation for beta2 requirement

* Fixed phpstan errors

* Fixed phpstan errors

* Fixed phpstan errors

* Fixed phpstan errors

* composer require propel/propel:2.0.0-beta2

* fixes related to beta2 upgrade

* php 7.4 is required for propelorm/Propel2/tree/2.0.0-beta2

* fixes for Symfony 4.4

* bugfix: getPlatform may return null

* Conditional adding alias of profiler service

Co-authored-by: Davy Rolink <[email protected]>
# Conflicts:
#	.github/workflows/CI 6.0.yml
#	README.markdown
#	Tests/Fixtures/Model/Base/BookQuery.php
#	composer.json
  • Loading branch information
SkyFoxvn committed Sep 14, 2022
1 parent 7c0761d commit 58fa12e
Show file tree
Hide file tree
Showing 61 changed files with 612 additions and 525 deletions.
149 changes: 75 additions & 74 deletions Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Propel\Bundle\PropelBundle\Command;

use Propel\Bundle\PropelBundle\Service\SchemaLocator;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,30 +27,11 @@
*/
abstract class AbstractCommand extends Command
{
/**
* @var string
*/
protected $cacheDir = null;

/**
* @var BundleInterface
*/
protected $bundle = null;

/**
* @var InputInterface
*/
protected $input;

/**
* @var OutputInterface
*/
protected $output;

/**
* @var ContainerInterface
*/
private $container;
protected ?string $cacheDir = null;
protected ?BundleInterface $bundle = null;
protected InputInterface $input;
protected OutputInterface $output;
private ContainerInterface $container;

use FormattingHelpers;

Expand All @@ -62,13 +45,11 @@ public function __construct(ContainerInterface $container, $name = null)
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$kernel = $this->getApplication()->getKernel();

$this->input = $input;
$this->output = $output;
$this->cacheDir = $kernel->getCacheDir().'/propel';
$this->cacheDir = $this->getKernel()->getCacheDir().'/propel';

if ($input->hasArgument('bundle') && '@' === substr($input->getArgument('bundle'), 0, 1)) {
$this->bundle = $this
Expand All @@ -78,22 +59,21 @@ protected function initialize(InputInterface $input, OutputInterface $output)
}
}

public function getContainer() {
public function getContainer(): ContainerInterface
{
return $this->container;
}

/**
* Create all the files needed by Propel's commands.
*/
protected function setupBuildTimeFiles()
protected function setupBuildTimeFiles(): void
{
$kernel = $this->getApplication()->getKernel();

$fs = new Filesystem();
$fs->mkdir($this->cacheDir);

// collect all schemas
$this->copySchemas($kernel, $this->cacheDir);
$this->copySchemas($this->getKernel(), $this->cacheDir);

// propel.json
$this->createPropelConfigurationFile($this->cacheDir.'/propel.json');
Expand All @@ -104,13 +84,13 @@ protected function setupBuildTimeFiles()
* @param string $cacheDir The directory in which the schemas will
* be copied.
*/
protected function copySchemas(KernelInterface $kernel, $cacheDir)
protected function copySchemas(KernelInterface $kernel, string $cacheDir): void
{
$filesystem = new Filesystem();

/** @var array<string, array{?BundleInterface, \SplFileInfo}> $finalSchemas */
$finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
foreach ($finalSchemas as $schema) {
/** @var null|Bundle $bundle */
list($bundle, $finalSchema) = $schema;

if ($bundle) {
Expand Down Expand Up @@ -172,7 +152,7 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
} else {
$table['package'] = $this->getPackageFromApp((string)$table['namespace']);
}
} else {
} elseif (isset($database['package'])) {
$table['package'] = $database['package'];
}
}
Expand All @@ -184,13 +164,13 @@ protected function copySchemas(KernelInterface $kernel, $cacheDir)
/**
* Return a list of final schema files that will be processed.
*
* @param KernelInterface $kernel The application kernel.
* @param BundleInterface $bundle If given, only the bundle's schemas will
* be returned.
* @param KernelInterface $kernel The application kernel.
* @param BundleInterface|null $bundle If given, only the bundle's schemas will
* be returned.
*
* @return array A list of schemas.
* @return array<string, array{?BundleInterface, \SplFileInfo}> A list of schemas.
*/
protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
protected function getFinalSchemas(KernelInterface $kernel, ?BundleInterface $bundle = null): array
{
if (null !== $bundle) {
return $this->getSchemaLocator()->locateFromBundle($bundle);
Expand All @@ -202,14 +182,16 @@ protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bun
/**
* Run a Symfony command.
*
* @param Command $command The command to run.
* @param array $parameters An array of parameters to give to the command.
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
* @param Command $command The command to run.
* @param array<string, mixed> $parameters An array of parameters to give to the command.
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return int The command return code.
*
* @throws \Symfony\Component\Console\Exception\ExceptionInterface
*/
protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output)
protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output): int
{
// add the command's name to the parameters
array_unshift($parameters, $this->getName());
Expand Down Expand Up @@ -250,9 +232,9 @@ protected function runCommand(Command $command, array $parameters, InputInterfac
*
* @param string $file Should be 'propel.json'.
*/
protected function createPropelConfigurationFile($file)
protected function createPropelConfigurationFile(string $file): void
{
$propelConfig = $this->getContainer()->getParameter('propel.configuration');
$propelConfig = $this->getConfig();

//needed because because Propel2's configuration tree is a bit different
//propel.runtime.logging is PropelBundle feature only.
Expand All @@ -268,11 +250,11 @@ protected function createPropelConfigurationFile($file)
/**
* Translates a list of connection names to their DSN equivalents.
*
* @param array $connections The names.
* @param string[] $connections The names.
*
* @return array
* @return string[]
*/
protected function getConnections(array $connections)
protected function getConnections(array $connections): array
{
$dsnList = array();
foreach ($connections as $connection) {
Expand All @@ -287,11 +269,11 @@ protected function getConnections(array $connections)
*
* @param string $name The connection name.
*
* @return array The connection data.
* @return array<string, mixed> The connection data.
*/
protected function getConnectionData($name)
protected function getConnectionData(string $name): array
{
$knownConnections = $this->getContainer()->getParameter('propel.configuration');
$knownConnections = $this->getConfig();
if (!isset($knownConnections['database']['connections'][$name])) {
throw new \InvalidArgumentException(sprintf('Unknown connection "%s"', $name));
}
Expand All @@ -306,7 +288,7 @@ protected function getConnectionData($name)
*
* @return string The DSN.
*/
protected function getDsn($connectionName)
protected function getDsn(string $connectionName): string
{
$connection = $this->getConnectionData($connectionName);
// Add user and password to dsn string
Expand All @@ -322,19 +304,22 @@ protected function getDsn($connectionName)
}

/**
* @return \Symfony\Component\Config\FileLocatorInterface
* @return SchemaLocator
*/
protected function getSchemaLocator()
protected function getSchemaLocator(): SchemaLocator
{
return $this->getContainer()->get('propel.schema_locator');
/** @var SchemaLocator $obj */
$obj = $this->getContainer()->get('propel.schema_locator');

return $obj;
}

/**
* @param string $namespace
*
* @return string
*/
protected function getPackageFromApp($namespace)
protected function getPackageFromApp(string $namespace): string
{
if ('\\' === $namespace[0]) {
$namespace = substr($namespace, 1);
Expand All @@ -348,12 +333,12 @@ protected function getPackageFromApp($namespace)
}

/**
* @param Bundle $bundle
* @param BundleInterface $bundle
* @param string $namespace
*
* @return string
*/
protected function getPackageFromBundle(Bundle $bundle, $namespace)
protected function getPackageFromBundle(BundleInterface $bundle, string $namespace): string
{
//find relative path from namespace to bundle->getNamespace()
$baseNamespace = (new \ReflectionClass($bundle))->getNamespaceName();
Expand All @@ -368,7 +353,7 @@ protected function getPackageFromBundle(Bundle $bundle, $namespace)
$namespaceDiff = substr($namespace, strlen($baseNamespace) + 1);

$bundlePath = realpath($bundle->getPath()) . '/' . str_replace('\\', '/', $namespaceDiff);
$appPath = realpath($this->getApplication()->getKernel()->getProjectDir());
$appPath = realpath($this->getKernel()->getProjectDir());

$path = static::getRelativePath($bundlePath, $appPath);

Expand All @@ -388,7 +373,7 @@ protected function getPackageFromBundle(Bundle $bundle, $namespace)
*
* @return string The current Propel cache directory.
*/
protected function getCacheDir()
protected function getCacheDir(): string
{
return $this->cacheDir;
}
Expand All @@ -401,7 +386,7 @@ protected function getCacheDir()
*
* @return string relative path without trailing slash
*/
public static function getRelativePath($from, $to)
public static function getRelativePath(string $from, string $to): string
{
$from = '/' . trim($from, '/');
$to = '/' . trim($to, '/');
Expand All @@ -422,10 +407,10 @@ public static function getRelativePath($from, $to)
/**
* Extract the database name from a given DSN
*
* @param string $dsn A DSN
* @return string The database name extracted from the given DSN
* @param string $dsn A DSN
* @return ?string The database name extracted from the given DSN
*/
protected function parseDbName($dsn)
protected function parseDbName(string $dsn): ?string
{
preg_match('#(dbname|Database)=([a-zA-Z0-9\_]+)#', $dsn, $matches);

Expand All @@ -442,9 +427,9 @@ protected function parseDbName($dsn)
*
* @return string
*/
protected function getMigrationsTable()
protected function getMigrationsTable(): string
{
$config = $this->getContainer()->getParameter('propel.configuration');
$config = $this->getConfig();

return $config['migrations']['tableName'];
}
Expand All @@ -454,22 +439,38 @@ protected function getMigrationsTable()
*
* @return string
*/
protected function getDefaultConnection()
protected function getDefaultConnection(): string
{
$config = $this->getContainer()->getParameter('propel.configuration');
$config = $this->getConfig();

return !empty($config['generator']['defaultConnection']) ? $config['generator']['defaultConnection'] : key($config['database']['connections']);
}

/**
* Reads the platform class from the configuration
*
* @return string The platform class name.
* @return string|null The platform class name.
*/
protected function getPlatform()
protected function getPlatform(): ?string
{
$config = $this->getContainer()->getParameter('propel.configuration');
$config = $this->getConfig();

return $config['generator']['platformClass'];
}

protected function getKernel(): KernelInterface
{
/** @var Application $application */
$application = $this->getApplication();

return $application->getKernel();
}

/**
* @return array{paths: array{schemaDir: string, sqlDir: string, migrationDir: string, composerDir: ?string, loaderScriptDir: string}, database: array{connections: array<string, array<string, mixed>>}, runtime: array{defaultConnection: string, logging: bool}, migrations: array{tableName: string, parserClass: string}, generator: array<string, mixed>}
*/
protected function getConfig(): array
{
return $this->getContainer()->getParameter('propel.configuration');
}
}
6 changes: 3 additions & 3 deletions Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BuildCommand extends Command
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this
->setName('propel:build')
Expand All @@ -42,9 +42,9 @@ protected function configure()
/**
* @see Command
*
* @throws \InvalidArgumentException When the target directory does not exist
* @throws \Symfony\Component\Console\Exception\ExceptionInterface
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$input->getOption('sql')) {
$in = new ArrayInput(array(
Expand Down
Loading

0 comments on commit 58fa12e

Please sign in to comment.