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

Fix argument type for RedirectTrait methods #753

Merged
merged 2 commits into from
Jan 27, 2025
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
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 @@
$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;

Check warning on line 136 in src/Error/ExceptionRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Error/ExceptionRenderer.php#L136

Added line #L136 was not covered by tests
}

$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 @@

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 @@

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

Check warning on line 106 in src/Listener/ApiQueryLogListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ApiQueryLogListener.php#L106

Added line #L106 was not covered by tests
}

$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 @@
*/
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));

Check warning on line 55 in src/Traits/RedirectTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/RedirectTrait.php#L55

Added line #L55 was not covered by tests
}

/**
* 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