Skip to content

Commit

Permalink
Remove constructor overriding.
Browse files Browse the repository at this point in the history
Normalize listeners and actions config in `setConfig()`.
  • Loading branch information
ADmad committed Oct 21, 2024
1 parent f52d017 commit a6d4a79
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/Controller/Component/CrudComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Crud\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Datasource\EntityInterface;
Expand Down Expand Up @@ -138,22 +137,46 @@ class CrudComponent extends Component
];

/**
* Constructor
* Initialize the component
*
* @param \Cake\Controller\ComponentRegistry<\Cake\Controller\Controller> $collection A ComponentCollection this component
* can use to lazy load its components.
* @param array $config Array of configuration settings.
* @param array $config Configuration values for component.
* @return void
*/
public function __construct(ComponentRegistry $collection, array $config = [])
public function initialize(array $config): void
{
$config += ['actions' => [], 'listeners' => []];
$config['actions'] = $this->normalizeArray($config['actions']);
$config['listeners'] = $this->normalizeArray($config['listeners']);
parent::initialize($config);

$this->_controller = $collection->getController();
$this->_controller = $this->getController();
$this->_eventManager = $this->_controller->getEventManager();
$this->_action = $this->_controller->getRequest()->getParam('action');
}

parent::__construct($collection, $config);
/**
* Set component config.
*
* It normalizes the 'actions' and 'listeners' arrays.
*
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
* @param mixed|null $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
* @return $this
*/
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
{
if (is_array($key)) {
if (isset($key['actions'])) {
$key['actions'] = $this->normalizeArray($key['actions']);
}
if (isset($key['listeners'])) {
$key['listeners'] = $this->normalizeArray($key['listeners']);
}
} elseif ($key === 'actions') {
$value = $this->normalizeArray($value);

Check failure on line 174 in src/Controller/Component/CrudComponent.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyNullArgument

src/Controller/Component/CrudComponent.php:174:44: PossiblyNullArgument: Argument 1 of Crud\Controller\Component\CrudComponent::normalizeArray cannot be null, possibly null value provided (see https://psalm.dev/078)
} elseif ($key === 'listeners') {
$value = $this->normalizeArray($value);

Check failure on line 176 in src/Controller/Component/CrudComponent.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PossiblyNullArgument

src/Controller/Component/CrudComponent.php:176:44: PossiblyNullArgument: Argument 1 of Crud\Controller\Component\CrudComponent::normalizeArray cannot be null, possibly null value provided (see https://psalm.dev/078)
}

return parent::setConfig($key, $value, $merge);
}

/**
Expand Down Expand Up @@ -182,19 +205,6 @@ public function normalizeArray(array $array): array
return $normal;
}

/**
* Add self to list of components capable of dispatching an action.
*
* @param array $config Configuration values for component.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);

$this->_action = $this->getController()->getRequest()->getParam('action');
}

/**
* Loads listeners
*
Expand Down

0 comments on commit a6d4a79

Please sign in to comment.