Skip to content

Use constructor property promotion in module Sample Data #37224

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

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,16 @@ class SampleDataDeployCommand extends Command
{
public const OPTION_NO_UPDATE = 'no-update';

/** @var Filesystem */
private Filesystem $filesystem;

/** @var Dependency */
private Dependency $sampleDataDependency;

/** @var ApplicationFactory */
private ApplicationFactory $applicationFactory;

/**
* @param Filesystem $filesystem
* @param Dependency $sampleDataDependency
* @param ApplicationFactory $applicationFactory
*/
public function __construct(
Filesystem $filesystem,
Dependency $sampleDataDependency,
ApplicationFactory $applicationFactory
private readonly Filesystem $filesystem,
private readonly Dependency $sampleDataDependency,
private readonly ApplicationFactory $applicationFactory
) {
$this->filesystem = $filesystem;
$this->sampleDataDependency = $sampleDataDependency;
$this->applicationFactory = $applicationFactory;

parent::__construct();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,18 @@ class SampleDataRemoveCommand extends Command
{
public const OPTION_NO_UPDATE = 'no-update';

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var Dependency
*/
private $sampleDataDependency;

/**
* @var ArrayInputFactory
* @deprecated 100.1.0
*/
private $arrayInputFactory; // phpcs:ignore Magento2.Commenting.ClassPropertyPHPDocFormatting

/**
* @var ApplicationFactory
*/
private $applicationFactory;

/**
* @param Filesystem $filesystem
* @param Dependency $sampleDataDependency
* @param ArrayInputFactory $arrayInputFactory
* @param ArrayInputFactory $arrayInputFactory @deprecated 100.1.0
* @param ApplicationFactory $applicationFactory
*/
public function __construct(
Filesystem $filesystem,
Dependency $sampleDataDependency,
ArrayInputFactory $arrayInputFactory,
ApplicationFactory $applicationFactory
private readonly Filesystem $filesystem,
private readonly Dependency $sampleDataDependency,
private readonly ArrayInputFactory $arrayInputFactory,
private readonly ApplicationFactory $applicationFactory
) {
$this->filesystem = $filesystem;
$this->sampleDataDependency = $sampleDataDependency;
$this->arrayInputFactory = $arrayInputFactory;
$this->applicationFactory = $applicationFactory;
parent::__construct();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,16 @@
*/
class SampleDataResetCommand extends Command
{
/**
* @var Dependency
*/
private $sampleDataDependency;

/**
* @var ModuleResource
*/
private $moduleResource;

/**
* @var PackageInfo
*/
private $packageInfo;

/**
* @param Dependency $sampleDataDependency
* @param ModuleResource $moduleResource
* @param PackageInfo $packageInfo
*/
public function __construct(
Dependency $sampleDataDependency,
ModuleResource $moduleResource,
PackageInfo $packageInfo
private readonly Dependency $sampleDataDependency,
private readonly ModuleResource $moduleResource,
private readonly PackageInfo $packageInfo
) {
$this->sampleDataDependency = $sampleDataDependency;
$this->moduleResource = $moduleResource;
$this->packageInfo = $packageInfo;
parent::__construct();
}

Expand Down
27 changes: 12 additions & 15 deletions app/code/Magento/SampleData/Console/CommandList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
*/
namespace Magento\SampleData\Console;

use Exception;
use Magento\Framework\Console\CommandListInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\SampleData\Console\Command\SampleDataDeployCommand;
use Magento\SampleData\Console\Command\SampleDataRemoveCommand;

/**
* Class CommandList
*/
class CommandList implements \Magento\Framework\Console\CommandListInterface
class CommandList implements CommandListInterface
{
/**
* Object Manager
*
* @var ObjectManagerInterface
* @param ObjectManagerInterface $objectManager Object Manager
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param ObjectManagerInterface $objectManager Object Manager
* @param ObjectManagerInterface $objectManager

*/
private $objectManager;

/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
public function __construct(
private readonly ObjectManagerInterface $objectManager
) {
}

/**
Expand All @@ -35,8 +32,8 @@ public function __construct(ObjectManagerInterface $objectManager)
protected function getCommandsClasses()
{
return [
\Magento\SampleData\Console\Command\SampleDataDeployCommand::class,
\Magento\SampleData\Console\Command\SampleDataRemoveCommand::class
SampleDataDeployCommand::class,
SampleDataRemoveCommand::class
];
}

Expand All @@ -50,7 +47,7 @@ public function getCommands()
if (class_exists($class)) {
$commands[] = $this->objectManager->get($class);
} else {
throw new \Exception('Class ' . $class . ' does not exist');
throw new Exception('Class ' . $class . ' does not exist');
}
}
return $commands;
Expand Down
46 changes: 13 additions & 33 deletions app/code/Magento/SampleData/Model/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Magento\Framework\Composer\ComposerInformation;
use Magento\Framework\Config\Composer\Package;
use Magento\Framework\Config\Composer\PackageFactory;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use stdClass;

/**
* Sample Data dependency
Expand All @@ -24,46 +27,23 @@ class Dependency
*/
public const SAMPLE_DATA_SUGGEST = 'Sample Data version:';

/**
* @var ComposerInformation
*/
protected $composerInformation;

/**
* @var PackageFactory
*/
private $packageFactory;

/**
* @var ComponentRegistrarInterface
*/
private $componentRegistrar;

/**
* @var ReadFactory
*/
private $directoryReadFactory;

/**
* Initialize dependencies.
*
* @param ComposerInformation $composerInformation
* @param Filesystem $filesystem @deprecated 2.3.0 $directoryReadFactory is used instead
* @param PackageFactory $packageFactory
* @param ComponentRegistrarInterface $componentRegistrar
* @param Filesystem\Directory\ReadFactory|null $directoryReadFactory
* @param ReadFactory|null $directoryReadFactory
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
ComposerInformation $composerInformation,
protected readonly ComposerInformation $composerInformation,
Filesystem $filesystem,
Copy link
Member

Choose a reason for hiding this comment

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

Should this be removed? It does not appear to be used.

Suggested change
Filesystem $filesystem,

PackageFactory $packageFactory,
ComponentRegistrarInterface $componentRegistrar,
\Magento\Framework\Filesystem\Directory\ReadFactory $directoryReadFactory = null
private readonly PackageFactory $packageFactory,
private readonly ComponentRegistrarInterface $componentRegistrar,
private ?ReadFactory $directoryReadFactory = null
) {
$this->composerInformation = $composerInformation;
$this->packageFactory = $packageFactory;
$this->componentRegistrar = $componentRegistrar;
$this->directoryReadFactory = $directoryReadFactory ?:
ObjectManager::getInstance()->get(ReadFactory::class);
}
Expand All @@ -72,7 +52,7 @@ public function __construct(
* Retrieve list of sample data packages from suggests
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getSampleDataPackages()
{
Expand All @@ -91,7 +71,7 @@ public function getSampleDataPackages()
* Retrieve suggested sample data packages from modules composer.json
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
protected function getSuggestsFromModules()
{
Expand All @@ -111,7 +91,7 @@ protected function getSuggestsFromModules()
*
* @param string $moduleDir
* @return Package
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
private function getModuleComposerPackage($moduleDir)
{
Expand All @@ -122,13 +102,13 @@ private function getModuleComposerPackage($moduleDir)
* see: https://github.com/php-pds/skeleton
*/
foreach ([$moduleDir, $moduleDir . DIRECTORY_SEPARATOR . '..'] as $dir) {
/** @var Filesystem\Directory\ReadInterface $directory */
/** @var ReadInterface $directory */
$directory = $this->directoryReadFactory->create($dir);
if ($directory->isExist('composer.json') && $directory->isReadable('composer.json')) {
/** @var Package $package */
return $this->packageFactory->create(['json' => json_decode($directory->readFile('composer.json'))]);
}
}
return $this->packageFactory->create(['json' => new \stdClass]);
return $this->packageFactory->create(['json' => new stdClass]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,26 @@
namespace Magento\SampleData\Setup\Patch\Data;

use Magento\Framework\Setup;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\SampleData\State;

/**
* Class ClearSampleDataState
* @package Magento\SampleData\Setup\Patch
*/
class ClearSampleDataState implements DataPatchInterface, PatchVersionInterface
{
/**
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var Setup\SampleData\State
*/
private $state;

/**
* ClearSampleDataState constructor.
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
* @param Setup\SampleData\State $state
* @param ModuleDataSetupInterface $moduleDataSetup
* @param State $state
*/
public function __construct(
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
\Magento\Framework\Setup\SampleData\State $state
private readonly ModuleDataSetupInterface $moduleDataSetup,
private readonly State $state
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->state = $state;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion app/code/Magento/SampleData/cli_commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* See COPYING.txt for license details.
*/

use Magento\Framework\Console\CommandLocator;
use Magento\SampleData\Console\CommandList;

if (PHP_SAPI == 'cli') {
\Magento\Framework\Console\CommandLocator::register(\Magento\SampleData\Console\CommandList::class);
CommandLocator::register(CommandList::class);
}
6 changes: 5 additions & 1 deletion app/code/Magento/SampleData/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_SampleData', __DIR__);
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magento_SampleData',
__DIR__
);