Skip to content

Commit 0803fce

Browse files
authored
Merge pull request #751 from FriendsOfCake/task/crudcomponent-constructor
Remove constructor overriding.
2 parents f52d017 + ce1f1bc commit 0803fce

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

psalm-baseline.xml

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.x-dev@">
2+
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
3+
<file src="src/Controller/Component/CrudComponent.php">
4+
<RedundantPropertyInitializationCheck>
5+
<code><![CDATA[$this->_action]]></code>
6+
<code><![CDATA[$this->_controller]]></code>
7+
<code><![CDATA[$this->_controller->getEventManager()]]></code>
8+
<code><![CDATA[$this->_controller->getRequest()->getParam('action')]]></code>
9+
<code><![CDATA[$this->_eventManager]]></code>
10+
<code><![CDATA[$this->getController()]]></code>
11+
</RedundantPropertyInitializationCheck>
12+
</file>
313
<file src="src/Listener/ApiQueryLogListener.php">
4-
<InternalMethod occurrences="1">
5-
<code>new QueryLogger()</code>
14+
<InternalMethod>
15+
<code><![CDATA[new QueryLogger()]]></code>
616
</InternalMethod>
717
</file>
818
<file src="src/Log/QueryLogger.php">
9-
<InternalClass occurrences="2">
10-
<code>CakeQueryLogger</code>
11-
<code>parent::log($level, $message, $context)</code>
19+
<InternalClass>
20+
<code><![CDATA[CakeQueryLogger]]></code>
21+
<code><![CDATA[parent::log($level, $message, $context)]]></code>
1222
</InternalClass>
13-
<InternalMethod occurrences="1">
14-
<code>parent::log($level, $message, $context)</code>
23+
<InternalMethod>
24+
<code><![CDATA[parent::log($level, $message, $context)]]></code>
1525
</InternalMethod>
1626
</file>
1727
</files>

src/Controller/Component/CrudComponent.php

+37-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Crud\Controller\Component;
55

66
use Cake\Controller\Component;
7-
use Cake\Controller\ComponentRegistry;
87
use Cake\Controller\Controller;
98
use Cake\Core\App;
109
use Cake\Datasource\EntityInterface;
@@ -138,22 +137,48 @@ class CrudComponent extends Component
138137
];
139138

140139
/**
141-
* Constructor
140+
* Initialize the component
142141
*
143-
* @param \Cake\Controller\ComponentRegistry<\Cake\Controller\Controller> $collection A ComponentCollection this component
144-
* can use to lazy load its components.
145-
* @param array $config Array of configuration settings.
142+
* @param array $config Configuration values for component.
143+
* @return void
146144
*/
147-
public function __construct(ComponentRegistry $collection, array $config = [])
145+
public function initialize(array $config): void
148146
{
149-
$config += ['actions' => [], 'listeners' => []];
150-
$config['actions'] = $this->normalizeArray($config['actions']);
151-
$config['listeners'] = $this->normalizeArray($config['listeners']);
147+
parent::initialize($config);
152148

153-
$this->_controller = $collection->getController();
154-
$this->_eventManager = $this->_controller->getEventManager();
149+
$this->_controller ??= $this->getController();
150+
$this->_eventManager ??= $this->_controller->getEventManager();
151+
$this->_action ??= $this->_controller->getRequest()->getParam('action');
152+
}
155153

156-
parent::__construct($collection, $config);
154+
/**
155+
* Set component config.
156+
*
157+
* It normalizes the 'actions' and 'listeners' arrays.
158+
*
159+
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
160+
* @param mixed|null $value The value to set.
161+
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
162+
* @return $this
163+
*/
164+
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
165+
{
166+
if (is_array($key)) {
167+
if (isset($key['actions'])) {
168+
$key['actions'] = $this->normalizeArray($key['actions']);
169+
}
170+
if (isset($key['listeners'])) {
171+
$key['listeners'] = $this->normalizeArray($key['listeners']);
172+
}
173+
} elseif ($key === 'actions') {
174+
assert(is_array($value));
175+
$value = $this->normalizeArray($value);
176+
} elseif ($key === 'listeners') {
177+
assert(is_array($value));
178+
$value = $this->normalizeArray($value);
179+
}
180+
181+
return parent::setConfig($key, $value, $merge);
157182
}
158183

159184
/**
@@ -182,19 +207,6 @@ public function normalizeArray(array $array): array
182207
return $normal;
183208
}
184209

185-
/**
186-
* Add self to list of components capable of dispatching an action.
187-
*
188-
* @param array $config Configuration values for component.
189-
* @return void
190-
*/
191-
public function initialize(array $config): void
192-
{
193-
parent::initialize($config);
194-
195-
$this->_action = $this->getController()->getRequest()->getParam('action');
196-
}
197-
198210
/**
199211
* Loads listeners
200212
*

0 commit comments

Comments
 (0)