Skip to content

Commit

Permalink
TASK: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kdambekalns committed Dec 6, 2019
1 parent 75d3502 commit 8c07dd5
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 166 deletions.
99 changes: 51 additions & 48 deletions Neos.Flow/Classes/Command/DoctrineCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Util\Debug;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\ORM\Tools\ToolsException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Cli\Exception\StopCommandException;
use Neos\Flow\Error\Debugger;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\Exception\StopActionException;
use Neos\Flow\Package;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Persistence\Doctrine\Service as DoctrineService;
use Neos\Flow\Utility\Exception as UtilityException;
use Neos\Utility\Exception\FilesException;
use Neos\Utility\Files;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -67,15 +70,15 @@ class DoctrineCommandController extends CommandController
* @param array $settings
* @return void
*/
public function injectSettings(array $settings)
public function injectSettings(array $settings): void
{
$this->settings = $settings['persistence'];
}

/**
* @param LoggerInterface $logger
*/
public function injectLogger(LoggerInterface $logger)
public function injectLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
Expand All @@ -84,9 +87,11 @@ public function injectLogger(LoggerInterface $logger)
* Compile the Doctrine proxy classes
*
* @return void
* @throws UtilityException
* @throws FilesException
* @Flow\Internal
*/
public function compileProxiesCommand()
public function compileProxiesCommand(): void
{
$this->doctrineService->compileProxies();
}
Expand All @@ -102,10 +107,10 @@ public function compileProxiesCommand()
* any way.
*
* @return void
* @throws StopCommandException
* @see neos.flow:doctrine:entitystatus
* @throws StopActionException
*/
public function validateCommand()
public function validateCommand(): void
{
$this->outputLine();
$classesAndErrors = $this->doctrineService->validateMapping();
Expand Down Expand Up @@ -133,11 +138,12 @@ public function validateCommand()
*
* @param string $output A file to write SQL to, instead of executing it
* @return void
* @throws ToolsException
* @throws StopCommandException
* @see neos.flow:doctrine:update
* @see neos.flow:doctrine:migrate
* @throws StopActionException
*/
public function createCommand(string $output = null)
public function createCommand(string $output = null): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Database schema creation has been SKIPPED, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand All @@ -162,11 +168,11 @@ public function createCommand(string $output = null)
* @param boolean $unsafeMode If set, foreign keys, sequences and tables can potentially be dropped.
* @param string $output A file to write SQL to, instead of executing the update directly
* @return void
* @throws StopCommandException
* @see neos.flow:doctrine:create
* @see neos.flow:doctrine:migrate
* @throws StopActionException
*/
public function updateCommand(bool $unsafeMode = false, string $output = null)
public function updateCommand(bool $unsafeMode = false, string $output = null): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Database schema update has been SKIPPED, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand All @@ -192,9 +198,10 @@ public function updateCommand(bool $unsafeMode = false, string $output = null)
* @param boolean $dumpMappingData If set, the mapping data will be output
* @param string $entityClassName If given, the mapping data for just this class will be output
* @return void
* @throws \Doctrine\ORM\ORMException
* @see neos.flow:doctrine:validate
*/
public function entityStatusCommand(bool $dumpMappingData = false, string $entityClassName = null)
public function entityStatusCommand(bool $dumpMappingData = false, string $entityClassName = null): void
{
$info = $this->doctrineService->getEntityStatus();

Expand All @@ -218,19 +225,17 @@ public function entityStatusCommand(bool $dumpMappingData = false, string $entit
$this->outputLine();
}
}
} else {
if (array_key_exists($entityClassName, $info) && $info[$entityClassName] instanceof ClassMetadata) {
$entityStatus = $info[$entityClassName];
$this->outputLine('<success>[OK]</success> %s', [$entityClassName]);
if ($dumpMappingData) {
Debugger::clearState();
$this->outputLine(Debugger::renderDump($entityStatus, 0, true, true));
}
} else {
$this->outputLine('<info>[FAIL]</info> %s', [$entityClassName]);
$this->outputLine('Class not found.');
$this->outputLine();
} elseif (array_key_exists($entityClassName, $info) && $info[$entityClassName] instanceof ClassMetadata) {
$entityStatus = $info[$entityClassName];
$this->outputLine('<success>[OK]</success> %s', [$entityClassName]);
if ($dumpMappingData) {
Debugger::clearState();
$this->outputLine(Debugger::renderDump($entityStatus, 0, true, true));
}
} else {
$this->outputLine('<info>[FAIL]</info> %s', [$entityClassName]);
$this->outputLine('Class not found.');
$this->outputLine();
}
}
}
Expand All @@ -247,10 +252,9 @@ public function entityStatusCommand(bool $dumpMappingData = false, string $entit
* @param integer $offset Offset the result by this number
* @param integer $limit Limit the result to this number
* @return void
* @throws \InvalidArgumentException
* @throws StopActionException
* @throws StopCommandException
*/
public function dqlCommand(int $depth = 3, string $hydrationMode = 'array', int $offset = null, int $limit = null)
public function dqlCommand(int $depth = 3, string $hydrationMode = 'array', int $offset = null, int $limit = null): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('DQL query is not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand Down Expand Up @@ -278,13 +282,13 @@ public function dqlCommand(int $depth = 3, string $hydrationMode = 'array', int
* @param boolean $showMigrations Output a list of all migrations and their status
* @param boolean $showDescriptions Show descriptions for the migrations (enables versions display)
* @return void
* @throws StopCommandException
* @see neos.flow:doctrine:migrate
* @see neos.flow:doctrine:migrationexecute
* @see neos.flow:doctrine:migrationgenerate
* @see neos.flow:doctrine:migrationversion
* @throws StopActionException
*/
public function migrationStatusCommand(bool $showMigrations = false, bool $showDescriptions = false)
public function migrationStatusCommand(bool $showMigrations = false, bool $showDescriptions = false): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Doctrine migration status not available, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand All @@ -309,13 +313,14 @@ public function migrationStatusCommand(bool $showMigrations = false, bool $showD
* @param boolean $dryRun Whether to do a dry run or not
* @param boolean $quiet If set, only the executed migration versions will be output, one per line
* @return void
* @throws MigrationException
* @throws StopCommandException
* @see neos.flow:doctrine:migrationstatus
* @see neos.flow:doctrine:migrationexecute
* @see neos.flow:doctrine:migrationgenerate
* @see neos.flow:doctrine:migrationversion
* @throws StopActionException
*/
public function migrateCommand(string $version = null, string $output = null, bool $dryRun = false, bool $quiet = false)
public function migrateCommand(string $version = null, string $output = null, bool $dryRun = false, bool $quiet = false): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand Down Expand Up @@ -346,7 +351,7 @@ public function migrateCommand(string $version = null, string $output = null, bo
* @return void
* @Flow\Signal
*/
protected function emitAfterDatabaseMigration()
protected function emitAfterDatabaseMigration(): void
{
}

Expand All @@ -360,13 +365,14 @@ protected function emitAfterDatabaseMigration()
* @param string $output A file to write SQL to, instead of executing it
* @param boolean $dryRun Whether to do a dry run or not
* @return void
* @throws MigrationException
* @throws StopCommandException
* @see neos.flow:doctrine:migrate
* @see neos.flow:doctrine:migrationstatus
* @see neos.flow:doctrine:migrationgenerate
* @see neos.flow:doctrine:migrationversion
* @throws StopActionException
*/
public function migrationExecuteCommand(string $version, string $direction = 'up', string $output = null, bool $dryRun = false)
public function migrationExecuteCommand(string $version, string $direction = 'up', string $output = null, bool $dryRun = false): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand All @@ -390,14 +396,13 @@ public function migrationExecuteCommand(string $version, string $direction = 'up
* @param boolean $add The migration to mark as migrated
* @param boolean $delete The migration to mark as not migrated
* @return void
* @throws \InvalidArgumentException
* @throws StopActionException
* @throws StopCommandException
* @see neos.flow:doctrine:migrate
* @see neos.flow:doctrine:migrationstatus
* @see neos.flow:doctrine:migrationexecute
* @see neos.flow:doctrine:migrationgenerate
*/
public function migrationVersionCommand(string $version, bool $add = false, bool $delete = false)
public function migrationVersionCommand(string $version, bool $add = false, bool $delete = false): void
{
if (!$this->isDatabaseConfigured()) {
$this->outputLine('Doctrine migration not possible, the driver and host backend options are not set in /Configuration/Settings.yaml.');
Expand Down Expand Up @@ -438,14 +443,16 @@ public function migrationVersionCommand(string $version, bool $add = false, bool
* @param string $filterExpression Only include tables/sequences matching the filter expression regexp
* @param boolean $force Generate migrations even if there are migrations left to execute
* @return void
* @throws StopActionException
* @throws \Neos\Utility\Exception\FilesException
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\ORM\ORMException
* @throws StopCommandException
* @throws FilesException
* @see neos.flow:doctrine:migrate
* @see neos.flow:doctrine:migrationstatus
* @see neos.flow:doctrine:migrationexecute
* @see neos.flow:doctrine:migrationversion
*/
public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string $filterExpression = null, bool $force = false)
public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string $filterExpression = null, bool $force = false): void
{
// "driver" is used only for Doctrine, thus we (mis-)use it here
// additionally, when no host is set, skip this step, assuming no DB is needed
Expand All @@ -468,7 +475,7 @@ public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string
}
}

list($status, $migrationClassPathAndFilename) = $this->doctrineService->generateMigration($diffAgainstCurrent, $filterExpression);
[$status, $migrationClassPathAndFilename] = $this->doctrineService->generateMigration($diffAgainstCurrent, $filterExpression);

$this->outputLine('<info>%s</info>', [$status]);
$this->outputLine();
Expand Down Expand Up @@ -512,9 +519,9 @@ public function migrationGenerateCommand(bool $diffAgainstCurrent = true, string
*
* @param \Exception $exception
* @return void
* @throws StopActionException
* @throws StopCommandException
*/
protected function handleException(\Exception $exception)
protected function handleException(\Exception $exception): void
{
$this->outputLine('<error>%s</error>', [$exception->getMessage()]);
$this->outputLine();
Expand All @@ -525,13 +532,9 @@ protected function handleException(\Exception $exception)
$this->quit(1);
}

protected function isDatabaseConfigured()
protected function isDatabaseConfigured(): bool
{
// "driver" is used only for Doctrine, thus we (mis-)use it here
if ($this->settings['backendOptions']['driver'] === null) {
return false;
}

return true;
return !($this->settings['backendOptions']['driver'] === null);
}
}
16 changes: 10 additions & 6 deletions Neos.Flow/Classes/Persistence/Doctrine/CountWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@
* with this package in the file License-BSD.txt. *
* */

use Doctrine\ORM\Query\AST\AggregateExpression;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\AST\SelectExpression;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\TreeWalkerAdapter;
use Neos\Flow\Annotations as Flow;

/**
* A walker to transform a select query into a count query.
*
* @Flow\Proxy(false)
*/
class CountWalker extends \Doctrine\ORM\Query\TreeWalkerAdapter
class CountWalker extends TreeWalkerAdapter
{
/**
* Walks down a SelectStatement AST node, modifying it to retrieve a COUNT
*
* @param \Doctrine\ORM\Query\AST\SelectStatement $AST
* @param SelectStatement $AST
* @return void
*/
public function walkSelectStatement(\Doctrine\ORM\Query\AST\SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST)
{
$parent = null;
$parentName = null;
foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) {
if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) {
if ($qComp['parent'] === null && $qComp['nestingLevel'] === 0) {
$parent = $qComp;
$parentName = $dqlAlias;
break;
Expand All @@ -43,8 +47,8 @@ public function walkSelectStatement(\Doctrine\ORM\Query\AST\SelectStatement $AST
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;

$AST->selectClause->selectExpressions = [
new \Doctrine\ORM\Query\AST\SelectExpression(
new \Doctrine\ORM\Query\AST\AggregateExpression('count', $pathExpression, true), null
new SelectExpression(
new AggregateExpression('count', $pathExpression, true), null
)
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Neos\Flow\Persistence\Doctrine\DataTypes;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\JsonArrayType as DoctrineJsonArrayType;
use Doctrine\ORM\Mapping\Entity as ORMEntity;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
Expand Down Expand Up @@ -59,7 +61,7 @@ public function getBindingType()
* Use jsonb for PostgreSQL, this means we require PostgreSQL 9.4
*
* @param array $fieldDeclaration The field declaration
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform
* @param AbstractPlatform $platform The currently used database platform
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
Expand Down Expand Up @@ -199,15 +201,15 @@ protected function encodeObjectReferences(array &$array)
];
} elseif ($value instanceof \SplObjectStorage) {
throw new \RuntimeException('SplObjectStorage in array properties is not supported', 1375196580);
} elseif ($value instanceof \Doctrine\Common\Collections\Collection) {
} elseif ($value instanceof Collection) {
throw new \RuntimeException('Collection in array properties is not supported', 1375196581);
} elseif ($value instanceof \ArrayObject) {
throw new \RuntimeException('ArrayObject in array properties is not supported', 1375196582);
} elseif ($this->persistenceManager->isNewObject($value) === false
&& (
$this->reflectionService->isClassAnnotatedWith($propertyClassName, Flow\Entity::class)
|| $this->reflectionService->isClassAnnotatedWith($propertyClassName, Flow\ValueObject::class)
|| $this->reflectionService->isClassAnnotatedWith($propertyClassName, \Doctrine\ORM\Mapping\Entity::class)
|| $this->reflectionService->isClassAnnotatedWith($propertyClassName, ORMEntity::class)
)
) {
$value = [
Expand Down
Loading

0 comments on commit 8c07dd5

Please sign in to comment.