Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve PHP 8 support w/ rector, removes legacy code, deprecates unused methods #1128

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Command/MakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
$dependencies = new DependencyBuilder();
$this->maker->configureDependencies($dependencies, $input);

if (!$dependencies->isPhpVersionSatisfied()) {
throw new RuntimeCommandException('The make:entity command requires that you use PHP 7.1 or higher.');
}

if ($missingPackagesMessage = $dependencies->getMissingPackagesMessage($this->getName())) {
throw new RuntimeCommandException($missingPackagesMessage);
}
Expand Down
19 changes: 9 additions & 10 deletions src/Console/MigrationDiffFilteredOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\OutputInterface;

if (\PHP_VERSION_ID < 80000
// look for the "string|iterable" type on OutputInterface::write()
|| !(new \ReflectionMethod(OutputInterface::class, 'write'))->getParameters()[0]->getType()) {
// look for the "string|iterable" type on OutputInterface::write()
// @legacy - Use MigrationDiffFilteredOutput_php8 when Symfony 5.4 is no longer supported
if (!(new \ReflectionMethod(OutputInterface::class, 'write'))->getParameters()[0]->getType()) {
class MigrationDiffFilteredOutput implements OutputInterface
{
use BaseMakerMigrationDiffFilteredOuputTrait;
Expand Down Expand Up @@ -47,13 +47,12 @@ public function setDecorated($decorated)

trait BaseMakerMigrationDiffFilteredOuputTrait
{
private $output;
private $buffer = '';
private $previousLineWasRemoved = false;
private string $buffer = '';
private bool $previousLineWasRemoved = false;

public function __construct(OutputInterface $output)
{
$this->output = $output;
public function __construct(
private OutputInterface $output,
) {
}

public function _write($messages, bool $newline = false, $options = 0)
Expand Down Expand Up @@ -144,7 +143,7 @@ private function filterMessages($messages, bool $newLine)

$this->previousLineWasRemoved = false;
foreach ($hiddenPhrases as $hiddenPhrase) {
if (false !== strpos($message, $hiddenPhrase)) {
if (str_contains($message, $hiddenPhrase)) {
$this->previousLineWasRemoved = true;
unset($messages[$key]);

Expand Down
10 changes: 4 additions & 6 deletions src/ConsoleStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
*/
final class ConsoleStyle extends SymfonyStyle
{
private $output;

public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;

public function __construct(
InputInterface $input,
private OutputInterface $output,
) {
parent::__construct($input, $output);
}

Expand Down
18 changes: 4 additions & 14 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

final class DependencyBuilder
{
private $dependencies = [];
private $devDependencies = [];

private $minimumPHPVersion = 70100;
private array $dependencies = [];
private array $devDependencies = [];

/**
* Add a dependency that will be reported if the given class is missing.
Expand Down Expand Up @@ -44,7 +42,7 @@ public function addClassDependency(string $class, string $package, bool $require

public function requirePHP71(): void
{
// no-op - MakerBundle now required PHP 7.1
trigger_deprecation('symfony/maker-bundle', 'v1.44.0', 'requirePHP71() is deprecated and will be removed in a future version.');
}

/**
Expand Down Expand Up @@ -110,14 +108,6 @@ public function getMissingPackagesMessage(string $commandName, $message = null):
return $message;
}

/**
* @internal
*/
public function isPhpVersionSatisfied(): bool
{
return \PHP_VERSION_ID >= $this->minimumPHPVersion;
}

private function getRequiredDependencyNames(array $dependencies): array
{
$packages = [];
Expand Down Expand Up @@ -149,6 +139,6 @@ private function calculateMissingDependencies(array $dependencies): array
return [];
}

return array_unique(array_merge($missingPackages, $missingOptionalPackages));
return array_unique([...$missingPackages, ...$missingOptionalPackages]);
}
}
7 changes: 1 addition & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('maker');
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('maker');
}
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
Expand Down
16 changes: 5 additions & 11 deletions src/Docker/DockerDatabaseServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,11 @@ public static function getSuggestedServiceVersion(string $name): string

public static function getMissingExtensionName(string $name): ?string
{
switch ($name) {
case 'mariadb':
case 'mysql':
$driver = 'mysql';
break;
case 'postgres':
$driver = 'pgsql';
break;
default:
self::throwInvalidDatabase($name);
}
$driver = match ($name) {
'mariadb', 'mysql' => 'mysql',
'postgres' => 'pgsql',
default => self::throwInvalidDatabase($name),
};

if (!\in_array($driver, \PDO::getAvailableDrivers(), true)) {
return $driver;
Expand Down
4 changes: 2 additions & 2 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool
try {
/** @var EntityManagerInterface $em */
$em = $this->getRegistry()->getManagerForClass($className);
} catch (\ReflectionException $exception) {
} catch (\ReflectionException) {
// this exception will be thrown by the registry if the class isn't created yet.
// an example case is the "make:entity" command, which needs to know which driver is used for the class to determine
// if the class should be generated with attributes or annotations. If this exception is thrown, we will check based on the
Expand Down Expand Up @@ -167,7 +167,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
if ($disconnected) {
try {
$loaded = $cmf->getAllMetadata();
} catch (ORMMappingException|PersistenceMappingException $e) {
} catch (ORMMappingException|PersistenceMappingException) {
$loaded = $this->isInstanceOf($cmf, AbstractClassMetadataFactory::class) ? $cmf->getLoadedMetadata() : [];
}

Expand Down
4 changes: 1 addition & 3 deletions src/Doctrine/EntityDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function getFormFields(): array

if (!empty($this->metadata->embeddedClasses)) {
foreach (array_keys($this->metadata->embeddedClasses) as $embeddedClassKey) {
$fields = array_filter($fields, function ($v) use ($embeddedClassKey) {
return 0 !== strpos($v, $embeddedClassKey.'.');
});
$fields = array_filter($fields, static fn ($v) => !str_starts_with($v, $embeddedClassKey.'.'));
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/Doctrine/EntityRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function regenerateEntities(string $classOrNamespace): void
{
try {
$metadata = $this->doctrineHelper->getMetadata($classOrNamespace);
} catch (MappingException|LegacyCommonMappingException|PersistenceMappingException $mappingException) {
} catch (MappingException|LegacyCommonMappingException|PersistenceMappingException) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctrine\Common namespaced imports e.g. LegacyCommonMappingException will be removed in a separate PR as there are a lot of changes related to outdated legacy classes that need to be made based on a git grep

$metadata = $this->doctrineHelper->getMetadata($classOrNamespace, true);
}

Expand Down Expand Up @@ -72,7 +72,7 @@ public function regenerateEntities(string $classOrNamespace): void
$embeddedClasses = [];

foreach ($classMetadata->embeddedClasses as $fieldName => $mapping) {
if (false !== strpos($fieldName, '.')) {
if (str_contains($fieldName, '.')) {
continue;
}

Expand All @@ -91,7 +91,7 @@ public function regenerateEntities(string $classOrNamespace): void

foreach ($classMetadata->fieldMappings as $fieldName => $mapping) {
// skip embedded fields
if (false !== strpos($fieldName, '.')) {
if (str_contains($fieldName, '.')) {
[$fieldName, $embeddedFiledName] = explode('.', $fieldName);

$operations[$embeddedClasses[$fieldName]]->addEntityField($embeddedFiledName, $mapping);
Expand Down Expand Up @@ -238,11 +238,11 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array
/** @var \ReflectionClass $classReflection */
$classReflection = $classMetadata->reflClass;

$targetFields = array_merge(
array_keys($classMetadata->fieldMappings),
array_keys($classMetadata->associationMappings),
array_keys($classMetadata->embeddedClasses)
);
$targetFields = [
...array_keys($classMetadata->fieldMappings),
...array_keys($classMetadata->associationMappings),
...array_keys($classMetadata->embeddedClasses),
];

if ($classReflection) {
// exclude traits
Expand All @@ -257,10 +257,8 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array
$targetFields = array_diff($targetFields, $traitProperties);

// exclude inherited properties
$targetFields = array_filter($targetFields, static function ($field) use ($classReflection) {
return $classReflection->hasProperty($field) &&
$classReflection->getProperty($field)->getDeclaringClass()->getName() == $classReflection->getName();
});
$targetFields = array_filter($targetFields, static fn ($field) => $classReflection->hasProperty($field) &&
$classReflection->getProperty($field)->getDeclaringClass()->getName() === $classReflection->getName());
}

return $targetFields;
Expand Down
2 changes: 1 addition & 1 deletion src/Event/ConsoleErrorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
final class ConsoleErrorSubscriber implements EventSubscriberInterface
{
private $setExitCode = false;
private bool $setExitCode = false;

public function onConsoleError(ConsoleErrorEvent $event): void
{
Expand Down
13 changes: 5 additions & 8 deletions src/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
class EventRegistry
{
// list of *known* events to always include (if they exist)
private static $newEventsMap = [
private static array $newEventsMap = [
'kernel.exception' => ExceptionEvent::class,
'kernel.request' => RequestEvent::class,
'kernel.response' => ResponseEvent::class,
Expand All @@ -53,7 +53,7 @@ class EventRegistry
'kernel.terminate' => TerminateEvent::class,
];

private static $eventsMap = [
private static array $eventsMap = [
'console.command' => ConsoleCommandEvent::class,
'console.terminate' => ConsoleTerminateEvent::class,
'console.error' => ConsoleErrorEvent::class,
Expand All @@ -71,12 +71,9 @@ class EventRegistry
'security.switch_user' => SwitchUserEvent::class,
];

private $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;

public function __construct(
private EventDispatcherInterface $eventDispatcher,
) {
// Loop through the new event classes
foreach (self::$newEventsMap as $eventName => $newEventClass) {
// Check if the new event classes exist, if so replace the old one with the new.
Expand Down
Loading