Skip to content

Commit

Permalink
Merge pull request #753 from FriendsOfCake/redirect-url
Browse files Browse the repository at this point in the history
Fix argument type for RedirectTrait methods
  • Loading branch information
ADmad authored Jan 27, 2025
2 parents 0803fce + 07720c6 commit b88e5ac
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ permissions:
jobs:
testsuite:
uses: ADmad/.github/.github/workflows/testsuite-with-db.yml@master
secrets: inherit
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

cs-stan:
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 6
level: 8
paths:
- src
excludePaths:
Expand Down
1 change: 1 addition & 0 deletions src/Action/AddAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ protected function _post(): ?Response
}

$saveCallback = [$this->_model(), $subject->saveMethod];
/** @phpstan-ignore argument.type */
if (call_user_func($saveCallback, $subject->entity, $subject->saveOptions)) {
return $this->_success($subject);
}
Expand Down
1 change: 1 addition & 0 deletions src/Action/EditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ protected function _put(string|int|null $id = null): ?Response
);

$this->_trigger('beforeSave', $subject);
/** @phpstan-ignore argument.type */
if (call_user_func([$this->_model(), $this->saveMethod()], $entity, $this->saveOptions())) {
return $this->_success($subject);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/Component/CrudComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,10 @@ public function defaults(string $type, array|string $name, mixed $config = null)
return null;
}

/** @psalm-suppress PossiblyInvalidArgument */
/**
* @psalm-suppress PossiblyInvalidArgument
* @phpstan-ignore argument.type
*/
return $this->getConfig(sprintf('%s.%s', $type, $name));
}

Expand Down Expand Up @@ -690,6 +693,7 @@ public function getSubject(array $additional = []): Subject
*/
protected function _loadListeners(): void
{
/** @var string $name */
foreach (array_keys($this->getConfig('listeners')) as $name) {
$this->_loadListener($name);
}
Expand Down
1 change: 1 addition & 0 deletions src/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* For full copyright and license information, please see the LICENSE.txt
*
* @property \Crud\Controller\Component\CrudComponent $Crud
* @phpstan-ignore trait.unused
*/
trait ControllerTrait
{
Expand Down
8 changes: 7 additions & 1 deletion src/Error/ExceptionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ protected function _getQueryLog(): array
$queryLog = [];
$sources = ConnectionManager::configured();
foreach ($sources as $source) {
$logger = ConnectionManager::get($source)->getDriver()->getLogger();
$driver = ConnectionManager::get($source)->getDriver();
if (!method_exists($driver, 'getLogger')) {
continue;
}

$logger = $driver->getLogger();
if ($logger && method_exists($logger, 'getLogs')) {
/** @var \Crud\Log\QueryLogger $logger */
$queryLog[$source] = $logger->getLogs();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Listener/ApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ protected function _ensureData(Subject $subject): void
}

if (method_exists($subject->entity, $valuePath)) {
/** @phpstan-ignore argument.type */
$data = Hash::insert($data, $keyPath, call_user_func([$subject->entity, $valuePath]));
} elseif (isset($subject->entity->{$valuePath})) {
$data = Hash::insert($data, $keyPath, $subject->entity->{$valuePath});
Expand Down
16 changes: 13 additions & 3 deletions src/Listener/ApiQueryLogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function setupLogging(EventInterface $event): void

foreach ($connections as $connectionName) {
try {
$connection = $this->_getSource($connectionName);
$connection->getDriver()->setLogger(new QueryLogger());
$driver = $this->_getSource($connectionName)->getDriver();
if (method_exists($driver, 'setLogger')) {
$driver->setLogger(new QueryLogger());
}
} catch (MissingDatasourceConfigException $e) {
//Safe to ignore this :-)
}
Expand Down Expand Up @@ -99,8 +101,16 @@ protected function _getQueryLogs(): array

$queryLog = [];
foreach ($sources as $source) {
$logger = $this->_getSource($source)->getDriver()->getLogger();
$driver = $this->_getSource($source)->getDriver();
if (!method_exists($driver, 'getLogger')) {
continue;
}

$logger = $driver->getLogger();
if (method_exists($logger, 'getLogs')) {
/**
* @var \Crud\Log\QueryLogger $logger
*/
$queryLog[$source] = $logger->getLogs();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Traits/FindMethodTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function _findRecord(string|int|null $id, Subject $subject): EntityInt
/**
* @psalm-suppress PossiblyInvalidArgument
* @psalm-suppress InvalidArrayOffset
* @phpstan-ignore argument.type
*/
$query->where([current($query->aliasField($repository->getPrimaryKey())) => $id]);

Expand Down
31 changes: 10 additions & 21 deletions src/Traits/RedirectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,35 @@ public function redirectConfig(?string $name = null, ?array $config = null): mix
*/
protected function _refererRedirectUrl(array|string|null $default = null): array|string
{
$controller = $this->_controller();

return $this->_redirectUrl($controller->referer($default, true));
return $this->_redirectUrl($this->_controller()->referer($default, true));
}

/**
* Returns the _redirect_url for this request.
*
* @param array|string|null $default Default URL to use if _redirect_url if not found in request or data.
* @param array|string $default Default URL to use if _redirect_url if not found in request or data.
* @return array|string
*/
protected function _redirectUrl(array|string|null $default = null): array|string
protected function _redirectUrl(array|string $default): array|string
{
$request = $this->_request();

if (!empty($request->getData('_redirect_url'))) {
return $request->getData('_redirect_url');
}
if (!empty($request->getQuery('_redirect_url'))) {
return $request->getQuery('_redirect_url');
}
if (!empty($request->getData('redirect_url'))) {
return $request->getData('redirect_url');
}
if (!empty($request->getQuery('redirect_url'))) {
return $request->getQuery('redirect_url');
}

return $default;
return $request->getData('_redirect_url')
?? $request->getQuery('_redirect_url')
?? $request->getData('redirect_url')
?? $request->getQuery('redirect_url')
?? $default;
}

/**
* Called for all redirects inside CRUD
*
* @param \Crud\Event\Subject $subject Event subject
* @param array|string|null $url URL
* @param array|string $url URL
* @param int $status Status code
* @return \Cake\Http\Response|null
*/
protected function _redirect(Subject $subject, string|array|null $url = null, int $status = 302): ?Response
protected function _redirect(Subject $subject, string|array $url, int $status = 302): ?Response
{
$url = $this->_redirectUrl($url);

Expand Down

0 comments on commit b88e5ac

Please sign in to comment.