Skip to content

Commit b88e5ac

Browse files
authored
Merge pull request #753 from FriendsOfCake/redirect-url
Fix argument type for RedirectTrait methods
2 parents 0803fce + 07720c6 commit b88e5ac

11 files changed

+43
-28
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ permissions:
1414
jobs:
1515
testsuite:
1616
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
17-
secrets: inherit
17+
secrets:
18+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1819

1920
cs-stan:
2021
uses: ADmad/.github/.github/workflows/cs-stan.yml@master

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 6
5+
level: 8
66
paths:
77
- src
88
excludePaths:

src/Action/AddAction.php

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ protected function _post(): ?Response
128128
}
129129

130130
$saveCallback = [$this->_model(), $subject->saveMethod];
131+
/** @phpstan-ignore argument.type */
131132
if (call_user_func($saveCallback, $subject->entity, $subject->saveOptions)) {
132133
return $this->_success($subject);
133134
}

src/Action/EditAction.php

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected function _put(string|int|null $id = null): ?Response
124124
);
125125

126126
$this->_trigger('beforeSave', $subject);
127+
/** @phpstan-ignore argument.type */
127128
if (call_user_func([$this->_model(), $this->saveMethod()], $entity, $this->saveOptions())) {
128129
return $this->_success($subject);
129130
}

src/Controller/Component/CrudComponent.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,10 @@ public function defaults(string $type, array|string $name, mixed $config = null)
607607
return null;
608608
}
609609

610-
/** @psalm-suppress PossiblyInvalidArgument */
610+
/**
611+
* @psalm-suppress PossiblyInvalidArgument
612+
* @phpstan-ignore argument.type
613+
*/
611614
return $this->getConfig(sprintf('%s.%s', $type, $name));
612615
}
613616

@@ -690,6 +693,7 @@ public function getSubject(array $additional = []): Subject
690693
*/
691694
protected function _loadListeners(): void
692695
{
696+
/** @var string $name */
693697
foreach (array_keys($this->getConfig('listeners')) as $name) {
694698
$this->_loadListener($name);
695699
}

src/Controller/ControllerTrait.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* For full copyright and license information, please see the LICENSE.txt
1515
*
1616
* @property \Crud\Controller\Component\CrudComponent $Crud
17+
* @phpstan-ignore trait.unused
1718
*/
1819
trait ControllerTrait
1920
{

src/Error/ExceptionRenderer.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,14 @@ protected function _getQueryLog(): array
131131
$queryLog = [];
132132
$sources = ConnectionManager::configured();
133133
foreach ($sources as $source) {
134-
$logger = ConnectionManager::get($source)->getDriver()->getLogger();
134+
$driver = ConnectionManager::get($source)->getDriver();
135+
if (!method_exists($driver, 'getLogger')) {
136+
continue;
137+
}
138+
139+
$logger = $driver->getLogger();
135140
if ($logger && method_exists($logger, 'getLogs')) {
141+
/** @var \Crud\Log\QueryLogger $logger */
136142
$queryLog[$source] = $logger->getLogs();
137143
}
138144
}

src/Listener/ApiListener.php

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ protected function _ensureData(Subject $subject): void
281281
}
282282

283283
if (method_exists($subject->entity, $valuePath)) {
284+
/** @phpstan-ignore argument.type */
284285
$data = Hash::insert($data, $keyPath, call_user_func([$subject->entity, $valuePath]));
285286
} elseif (isset($subject->entity->{$valuePath})) {
286287
$data = Hash::insert($data, $keyPath, $subject->entity->{$valuePath});

src/Listener/ApiQueryLogListener.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public function setupLogging(EventInterface $event): void
6464

6565
foreach ($connections as $connectionName) {
6666
try {
67-
$connection = $this->_getSource($connectionName);
68-
$connection->getDriver()->setLogger(new QueryLogger());
67+
$driver = $this->_getSource($connectionName)->getDriver();
68+
if (method_exists($driver, 'setLogger')) {
69+
$driver->setLogger(new QueryLogger());
70+
}
6971
} catch (MissingDatasourceConfigException $e) {
7072
//Safe to ignore this :-)
7173
}
@@ -99,8 +101,16 @@ protected function _getQueryLogs(): array
99101

100102
$queryLog = [];
101103
foreach ($sources as $source) {
102-
$logger = $this->_getSource($source)->getDriver()->getLogger();
104+
$driver = $this->_getSource($source)->getDriver();
105+
if (!method_exists($driver, 'getLogger')) {
106+
continue;
107+
}
108+
109+
$logger = $driver->getLogger();
103110
if (method_exists($logger, 'getLogs')) {
111+
/**
112+
* @var \Crud\Log\QueryLogger $logger
113+
*/
104114
$queryLog[$source] = $logger->getLogs();
105115
}
106116
}

src/Traits/FindMethodTrait.php

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected function _findRecord(string|int|null $id, Subject $subject): EntityInt
6666
/**
6767
* @psalm-suppress PossiblyInvalidArgument
6868
* @psalm-suppress InvalidArrayOffset
69+
* @phpstan-ignore argument.type
6970
*/
7071
$query->where([current($query->aliasField($repository->getPrimaryKey())) => $id]);
7172

src/Traits/RedirectTrait.php

+10-21
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,35 @@ public function redirectConfig(?string $name = null, ?array $config = null): mix
5252
*/
5353
protected function _refererRedirectUrl(array|string|null $default = null): array|string
5454
{
55-
$controller = $this->_controller();
56-
57-
return $this->_redirectUrl($controller->referer($default, true));
55+
return $this->_redirectUrl($this->_controller()->referer($default, true));
5856
}
5957

6058
/**
6159
* Returns the _redirect_url for this request.
6260
*
63-
* @param array|string|null $default Default URL to use if _redirect_url if not found in request or data.
61+
* @param array|string $default Default URL to use if _redirect_url if not found in request or data.
6462
* @return array|string
6563
*/
66-
protected function _redirectUrl(array|string|null $default = null): array|string
64+
protected function _redirectUrl(array|string $default): array|string
6765
{
6866
$request = $this->_request();
6967

70-
if (!empty($request->getData('_redirect_url'))) {
71-
return $request->getData('_redirect_url');
72-
}
73-
if (!empty($request->getQuery('_redirect_url'))) {
74-
return $request->getQuery('_redirect_url');
75-
}
76-
if (!empty($request->getData('redirect_url'))) {
77-
return $request->getData('redirect_url');
78-
}
79-
if (!empty($request->getQuery('redirect_url'))) {
80-
return $request->getQuery('redirect_url');
81-
}
82-
83-
return $default;
68+
return $request->getData('_redirect_url')
69+
?? $request->getQuery('_redirect_url')
70+
?? $request->getData('redirect_url')
71+
?? $request->getQuery('redirect_url')
72+
?? $default;
8473
}
8574

8675
/**
8776
* Called for all redirects inside CRUD
8877
*
8978
* @param \Crud\Event\Subject $subject Event subject
90-
* @param array|string|null $url URL
79+
* @param array|string $url URL
9180
* @param int $status Status code
9281
* @return \Cake\Http\Response|null
9382
*/
94-
protected function _redirect(Subject $subject, string|array|null $url = null, int $status = 302): ?Response
83+
protected function _redirect(Subject $subject, string|array $url, int $status = 302): ?Response
9584
{
9685
$url = $this->_redirectUrl($url);
9786

0 commit comments

Comments
 (0)