Skip to content

Commit

Permalink
Fix errors reported by static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 17, 2023
1 parent 44e0c53 commit d98efde
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ parameters:
- src
universalObjectCratesClasses:
- Crud\Event\Subject
bootstrapFiles:
- vendor/cakephp/cakephp/src/Database/Exception/DatabaseException.php
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@">
<file src="src/Listener/ViewListener.php">
<UndefinedPropertyAssignment occurrences="1">
<code>$event-&gt;getSubject()-&gt;element</code>
</UndefinedPropertyAssignment>
</file>
<file src="src/Listener/ViewSearchListener.php">
<PossiblyUndefinedArrayOffset occurrences="1">
<code>$input['type']</code>
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
usePhpDocMethodsWithoutMagicCall="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
4 changes: 2 additions & 2 deletions src/Listener/Traits/IndexTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function _getIndexTitleField(): string

$field = $action->getConfig('scaffold.index_title_field');
if ($field === null) {
$field = $this->_table()->getDisplayField();
$field = $this->_model()->getDisplayField();

Check warning on line 87 in src/Listener/Traits/IndexTypeTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/Traits/IndexTypeTrait.php#L87

Added line #L87 was not covered by tests
}

return $field;
Expand Down Expand Up @@ -154,5 +154,5 @@ abstract protected function _action(?string $name = null): BaseAction;
/**
* @inheritDoc
*/
abstract protected function _table();
abstract protected function _model();
}
23 changes: 14 additions & 9 deletions src/Listener/ViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
use CrudView\Listener\Traits\SiteTitleTrait;
use CrudView\Listener\Traits\UtilityNavigationTrait;
use CrudView\Traits\CrudViewConfigTrait;
use function Cake\Core\pluginSplit;
use function Cake\I18n\__d;

/**
* @method \Cake\ORM\Table _model()
*/
class ViewListener extends BaseListener
{
use CrudViewConfigTrait;
Expand Down Expand Up @@ -134,6 +138,7 @@ public function beforeRender(EventInterface $event): void
public function setFlash(EventInterface $event): void
{
unset($event->getSubject()->params['class']);
/** @psalm-suppress UndefinedPropertyAssignment */
$event->getSubject()->element = ltrim($event->getSubject()->type);
}

Expand Down Expand Up @@ -173,7 +178,7 @@ protected function _getPageTitle(): string
$displayFieldValue = $this->_displayFieldValue();
if (
$displayFieldValue === null
|| $this->_table()->getDisplayField() === $this->_table()->getPrimaryKey()
|| $this->_model()->getDisplayField() === $this->_model()->getPrimaryKey()

Check warning on line 181 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L181

Added line #L181 was not covered by tests
) {
/** @psalm-var string $primaryKeyValue */
return sprintf('%s %s #%s', $actionName, $controllerName, $primaryKeyValue);
Expand Down Expand Up @@ -218,12 +223,12 @@ protected function _getRelatedModels(array $associationTypes = []): array
if (empty($models)) {
$associations = [];
if (empty($associationTypes)) {
$associations = $this->_table()->associations();
$associations = $this->_model()->associations();

Check warning on line 226 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L226

Added line #L226 was not covered by tests
} else {
foreach ($associationTypes as $assocType) {
$associations = array_merge(
$associations,
$this->_table()->associations()->getByType($assocType)
$this->_model()->associations()->getByType($assocType)

Check warning on line 231 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L231

Added line #L231 was not covered by tests
);
}
}
Expand Down Expand Up @@ -268,7 +273,7 @@ protected function _blacklist(): array
*/
protected function _getPageVariables(): array
{
$table = $this->_table();
$table = $this->_model();

Check warning on line 276 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L276

Added line #L276 was not covered by tests
$modelClass = $table->getAlias();
$controller = $this->_controller();
$scope = $this->_action()->getConfig('scope');
Expand Down Expand Up @@ -317,7 +322,7 @@ protected function _scaffoldFields(array $associations = []): array
}

if (empty($scaffoldFields) || $action->getConfig('scaffold.autoFields')) {
$cols = $this->_table()->getSchema()->columns();
$cols = $this->_model()->getSchema()->columns();

Check warning on line 325 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L325

Added line #L325 was not covered by tests
$cols = Hash::normalize($cols);

$scope = $action->getConfig('scope');
Expand Down Expand Up @@ -554,7 +559,7 @@ protected function _normalizeActions(array $actions): array
*/
protected function _associations(array $whitelist = []): array
{
$table = $this->_table();
$table = $this->_model();

Check warning on line 562 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L562

Added line #L562 was not covered by tests

$associationConfiguration = [];

Expand Down Expand Up @@ -605,7 +610,7 @@ protected function _associations(array $whitelist = []): array
*/
protected function _primaryKeyValue(): array|string
{
$fields = (array)$this->_table()->getPrimaryKey();
$fields = (array)$this->_model()->getPrimaryKey();

Check warning on line 613 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L613

Added line #L613 was not covered by tests
$values = [];
foreach ($fields as $field) {
$values[] = $this->_deriveFieldFromContext($field);
Expand All @@ -628,7 +633,7 @@ protected function _primaryKeyValue(): array|string
protected function _displayFieldValue(): string|int|null
{
/** @psalm-suppress PossiblyInvalidArgument */
return $this->_deriveFieldFromContext($this->_table()->getDisplayField());
return $this->_deriveFieldFromContext($this->_model()->getDisplayField());

Check warning on line 636 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L636

Added line #L636 was not covered by tests
}

/**
Expand All @@ -641,7 +646,7 @@ protected function _displayFieldValue(): string|int|null
protected function _deriveFieldFromContext(string $field): mixed
{
$controller = $this->_controller();
$modelClass = $this->_table()->getAlias();
$modelClass = $this->_model()->getAlias();

Check warning on line 649 in src/Listener/ViewListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewListener.php#L649

Added line #L649 was not covered by tests
$entity = $this->_entity();
$request = $this->_request();
$value = $entity->get($field);
Expand Down
9 changes: 6 additions & 3 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Cake\Utility\Hash;
use Crud\Listener\BaseListener;

/**
* @method \Cake\ORM\Table _model()
*/
class ViewSearchListener extends BaseListener
{
/**
Expand Down Expand Up @@ -56,7 +59,7 @@ public function implementedEvents(): array
*/
public function afterPaginate(EventInterface $event): void
{
if (!$this->_table()->behaviors()->has('Search')) {
if (!$this->_model()->behaviors()->has('Search')) {

Check warning on line 62 in src/Listener/ViewSearchListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewSearchListener.php#L62

Added line #L62 was not covered by tests
return;
}

Expand All @@ -82,13 +85,13 @@ public function fields(): array
$fields = $this->getConfig('fields') ?: [];
$config = $this->getConfig();

$schema = $this->_table()->getSchema();
$schema = $this->_model()->getSchema();

Check warning on line 88 in src/Listener/ViewSearchListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewSearchListener.php#L88

Added line #L88 was not covered by tests
$request = $this->_request();

if ($fields) {
$fields = Hash::normalize($fields);
} else {
$filters = $this->_table()->searchManager()->getFilters($config['collection']);
$filters = $this->_model()->searchManager()->getFilters($config['collection']);

Check warning on line 94 in src/Listener/ViewSearchListener.php

View check run for this annotation

Codecov / codecov/patch

src/Listener/ViewSearchListener.php#L94

Added line #L94 was not covered by tests

foreach ($filters as $filter) {
$opts = $filter->getConfig('form');
Expand Down
1 change: 1 addition & 0 deletions src/View/Helper/CrudViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cake\Utility\Text;
use Cake\View\Helper;
use Cake\View\Helper\FormHelper;
use function Cake\Core\h;
use function Cake\I18n\__d;

/**
Expand Down

0 comments on commit d98efde

Please sign in to comment.