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

Remove constructor overriding. #751

Merged
merged 1 commit into from
Oct 21, 2024
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
26 changes: 18 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.x-dev@">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Controller/Component/CrudComponent.php">
<RedundantPropertyInitializationCheck>
<code><![CDATA[$this->_action]]></code>
<code><![CDATA[$this->_controller]]></code>
<code><![CDATA[$this->_controller->getEventManager()]]></code>
<code><![CDATA[$this->_controller->getRequest()->getParam('action')]]></code>
<code><![CDATA[$this->_eventManager]]></code>
<code><![CDATA[$this->getController()]]></code>
</RedundantPropertyInitializationCheck>
</file>
<file src="src/Listener/ApiQueryLogListener.php">
<InternalMethod occurrences="1">
<code>new QueryLogger()</code>
<InternalMethod>
<code><![CDATA[new QueryLogger()]]></code>
</InternalMethod>
</file>
<file src="src/Log/QueryLogger.php">
<InternalClass occurrences="2">
<code>CakeQueryLogger</code>
<code>parent::log($level, $message, $context)</code>
<InternalClass>
<code><![CDATA[CakeQueryLogger]]></code>
<code><![CDATA[parent::log($level, $message, $context)]]></code>
</InternalClass>
<InternalMethod occurrences="1">
<code>parent::log($level, $message, $context)</code>
<InternalMethod>
<code><![CDATA[parent::log($level, $message, $context)]]></code>
</InternalMethod>
</file>
</files>
62 changes: 37 additions & 25 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,48 @@ 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->_eventManager = $this->_controller->getEventManager();
$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') {
assert(is_array($value));
$value = $this->normalizeArray($value);
} elseif ($key === 'listeners') {
assert(is_array($value));
$value = $this->normalizeArray($value);
}

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

/**
Expand Down Expand Up @@ -182,19 +207,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
Loading