Skip to content

Commit

Permalink
PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Feb 16, 2024
1 parent f078212 commit 05c7d07
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 67 deletions.
26 changes: 9 additions & 17 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# Do not export those files in the Composer archive (lighter dependency)
.gitattributes export-ignore
.github/ export-ignore
.gitignore export-ignore
CHANGELOG.md export-ignore
README.md export-ignore
SECURITY.md export-ignore
composer.lock export-ignore
ecs.php export-ignore
package-lock.json export-ignore
package.json export-ignore
phpstan.neon export-ignore
stubs/ export-ignore
tests/ export-ignore
codeception.yml export-ignore
docs/ export-ignore
tests/ export-ignore
/.gitattributes export-ignore
/.github/ export-ignore
/.gitignore export-ignore
/ecs.php export-ignore
/phpstan.neon export-ignore
/lib/ export-ignore
/docs/ export-ignore
/tests/ export-ignore

# Auto-detect text files and perform LF normalization
# Auto detect text files and perform LF normalization
* text=auto
33 changes: 33 additions & 0 deletions lib/craft/behaviors/CustomFieldBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @link http://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license http://craftcms.com/license
*/

namespace craft\behaviors;

use yii\base\Behavior;

/**
* This file is never loaded at runtime. It’s only here for PHPStan’n sake.
*
* @internal
*/
class CustomFieldBehavior extends Behavior
{
/**
* @var bool Whether the behavior should provide methods based on the field handles.
*/
public bool $hasMethods = false;

/**
* @var bool Whether properties on the class should be settable directly.
*/
public bool $canSetProperties = true;

/**
* @var string[] List of supported field handles.
*/
public static $fieldHandles = [];
}
3 changes: 3 additions & 0 deletions lib/craft/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

define('CRAFT_BASE_PATH', __DIR__);
10 changes: 8 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ includes:
- vendor/craftcms/phpstan/phpstan.neon

parameters:
level: 1
level: 2
paths:
- src
- src
scanFiles:
- lib/craft/behaviors/CustomFieldBehavior.php
bootstrapFiles:
- lib/craft/bootstrap.php
excludePaths:
- src/services/migrate/Ether.php
7 changes: 3 additions & 4 deletions src/SeoFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace studioespresso\seofields;

use Craft;
use craft\base\Model;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
Expand Down Expand Up @@ -82,7 +83,7 @@ class SeoFields extends Plugin

// Public Properties
// =========================================================================
public string $schemaVersion = "2.0.0";
public string $schemaVersion = "4.0.0";


public const EVENT_SEOFIELDS_REGISTER_ELEMENT = "registerSeoElement";
Expand Down Expand Up @@ -178,9 +179,7 @@ public function getCpNavItem(): ?array

// Protected Methods
// =========================================================================
// Protected Methods
// =========================================================================
protected function createSettingsModel(): ?craft\base\Model
protected function createSettingsModel(): ?Model
{
return new Settings();
}
Expand Down
24 changes: 10 additions & 14 deletions src/controllers/CpApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class CpApiController extends Controller
public const NOT_FOUND_BASE = "seo-fields/cp-api/not-found";
public const REDIRECT_BASE = "seo-fields/cp-api/redirect";

/**
* @param null $siteHandle
* @return \yii\web\Response
*/
public function actionNotFound()
{
$sort = $this->request->getQueryParam('sort');
Expand Down Expand Up @@ -61,7 +57,7 @@ public function actionNotFound()
$formatter = Craft::$app->getFormatter();

foreach ($query->all() as $row) {
$lastHit = DateTimeHelper::toDateTime($row->dateLastHit);
$lastHit = DateTimeHelper::toDateTime($row->getAttribute('dateLastHit'));

$row = [
'id' => $row->getAttribute('id'),
Expand Down Expand Up @@ -141,17 +137,17 @@ public function actionRedirects()
];

foreach ($query->all() as $row) {
$lastHit = DateTimeHelper::toDateTime($row->dateLastHit);
$lastHit = DateTimeHelper::toDateTime($row->getAttribute('dateLastHit'));
$row = [
'url' => UrlHelper::cpUrl("seo-fields/redirects/edit/{$row->id}"),
'id' => $row->id,
'title' => $row->pattern,
'redirect' => $row->redirect,
'counter' => $row->counter,
'site' => !$row->siteId ? "All" : Craft::$app->getSites()->getSiteById($row->siteId)->name,
'url' => UrlHelper::cpUrl("seo-fields/redirects/edit/{$row->getAttribute('id')}"),
'id' => $row->getAttribute('id'),
'title' => $row->getAttribute('pattern'),
'redirect' => $row->getAttribute('redirect'),
'counter' => $row->getAttribute('counter'),
'site' => !$row->getAttribute('siteId') ? "All" : Craft::$app->getSites()->getSiteById($row->getAttribute('siteId'))->name,
'lastHit' => $lastHit ? $formatter->asDatetime($lastHit, Locale::LENGTH_SHORT) : "",
'method' => $row->method,
'matchType' => $types[$row->matchType],
'method' => $row->getAttribute('method'),
'matchType' => $types[$row->getAttribute('matchType')],
];

$rows[] = $row;
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/DefaultsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\helpers\Cp;
use craft\helpers\UrlHelper;
use craft\web\Controller;
use craft\web\Response;
use studioespresso\seofields\SeoFields;

class DefaultsController extends Controller
Expand All @@ -29,7 +30,7 @@ public function actionIndex()
}
}

public function actionSettings()
public function actionSettings(): Response
{
$site = $this->request->getRequiredQueryParam('site');
$currentSite = Craft::$app->getSites()->getSiteByHandle($site);
Expand Down
7 changes: 2 additions & 5 deletions src/controllers/NotFoundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
use craft\helpers\Cp;
use craft\helpers\UrlHelper;
use craft\web\Controller;
use craft\web\Response;
use studioespresso\seofields\SeoFields;

class NotFoundController extends Controller
{
/**
* @param null $siteHandle
* @return \yii\web\Response
*/
public function actionIndex()
public function actionIndex(): Response
{
$siteHandle = $this->request->getRequiredQueryParam('site');
$currentSite = Craft::$app->getSites()->getSiteByHandle($siteHandle);
Expand Down
1 change: 1 addition & 0 deletions src/controllers/RedirectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private function getHeaders($reader)
private function getRows(Reader $reader)
{
try {
/** @phpstan-ignore-next-line */
return $reader->fetchAll();
} catch (\Throwable $e) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/SchemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function actionIndex()
{
$primarySite = Craft::$app->getSites()->getPrimarySite();
$data = SeoFields::$plugin->defaultsService->getDataBySiteHandle($primarySite->handle);
$sections = Craft::$app->getSections()->getAllSections();
$sections = Craft::$app->getEntries()->getAllSections();

return $this->renderTemplate('seo-fields/_schema', [
'data' => $data,
Expand Down
8 changes: 4 additions & 4 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function addForeignKeys()
{
// $name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
$this->addForeignKey(
$this->db->getForeignKeyName(DefaultsRecord::tableName(), 'siteId'),
$this->db->getForeignKeyName(),
DefaultsRecord::tableName(),
'siteId',
'{{%sites}}',
Expand All @@ -132,7 +132,7 @@ protected function addForeignKeys()
);

$this->addForeignKey(
$this->db->getForeignKeyName(RedirectRecord::tableName(), 'siteId'),
$this->db->getForeignKeyName(),
RedirectRecord::tableName(),
'siteId',
'{{%sites}}',
Expand All @@ -141,7 +141,7 @@ protected function addForeignKeys()
);

$this->addForeignKey(
$this->db->getForeignKeyName(NotFoundRecord::tableName(), 'siteId'),
$this->db->getForeignKeyName(),
NotFoundRecord::tableName(),
'siteId',
'{{%sites}}',
Expand All @@ -150,7 +150,7 @@ protected function addForeignKeys()
);

$this->addForeignKey(
$this->db->getForeignKeyName(NotFoundRecord::tableName(), 'redirect'),
$this->db->getForeignKeyName(),
NotFoundRecord::tableName(),
'redirect',
'{{%seofields_redirects}}',
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/m191114_182559_addNotFoundTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function safeUp()

if ($tablesCreated) {
$this->addForeignKey(
$this->db->getForeignKeyName(NotFoundRecord::tableName(), 'siteId'),
$this->db->getForeignKeyName(),
NotFoundRecord::tableName(),
'siteId',
'{{%sites}}',
Expand Down
4 changes: 2 additions & 2 deletions src/migrations/m191114_201532_addRedirectsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public function safeUp()

if ($tablesCreated) {
$this->addForeignKey(
$this->db->getForeignKeyName(RedirectRecord::tableName(), 'siteId'),
$this->db->getForeignKeyName(),
RedirectRecord::tableName(),
'siteId',
'{{%sites}}',
'id',
'CASCADE'
);
$this->addForeignKey(
$this->db->getForeignKeyName(NotFoundRecord::tableName(), 'redirect'),
$this->db->getForeignKeyName(),
NotFoundRecord::tableName(),
'redirect',
'{{%seofields_redirects}}',
Expand Down
17 changes: 9 additions & 8 deletions src/models/SeoFieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function getSchema(Element $element = null)
return null;
}

/** @phpstan-ignore-next-line */
if (!$element->getShouldRenderSchema()) {
return null;
}
Expand All @@ -86,22 +87,22 @@ public function getSchema(Element $element = null)
$sectionId = $element->section->id;
$schemaClass = $schemaSettings[$sectionId];

/** @var $schema Schema */
/** @var Schema $schema */
$schema = \Craft::createObject($schemaClass);
$schema->name($this->getMetaTitle($element) ?? "");
$schema->description($this->getMetaDescription() ?? "");
$schema->url($element->getUrl() ?? "");
$schema->name($this->getMetaTitle($element) ?? ""); // @phpstan-ignore-line
$schema->description($this->getMetaDescription() ?? ""); // @phpstan-ignore-line
$schema->url($element->getUrl() ?? ""); // @phpstan-ignore-line
break;
case Category::class:
$schemaSettings = $settings['groups'];
$groupId = $element->group->id;
$schemaClass = $schemaSettings[$groupId];

/** @var $schema Schema */
/** @var Schema $schema */
$schema = Craft::createObject($schemaClass);
$schema->name($this->getMetaTitle($element) ?? "");
$schema->description($this->getMetaDescription() ?? "");
$schema->url($element->getUrl() ?? "");
$schema->name($this->getMetaTitle($element) ?? ""); // @phpstan-ignore-line
$schema->description($this->getMetaDescription() ?? ""); // @phpstan-ignore-line
$schema->url($element->getUrl() ?? ""); // @phpstan-ignore-line
break;
}
if ($schema) {
Expand Down
3 changes: 3 additions & 0 deletions src/records/RedirectRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @author Studio Espresso
* @package SeoFields
* @since 1.0.0
* @property $dateLastHit
* @property $pattern
* @property $redirect
*/
class RedirectRecord extends ActiveRecord
{
Expand Down
11 changes: 3 additions & 8 deletions src/services/NotFoundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use craft\models\Site;
use craft\web\Request;
use studioespresso\seofields\models\NotFoundModel;
use studioespresso\seofields\models\RedirectModel;
use studioespresso\seofields\records\NotFoundRecord;
use studioespresso\seofields\records\RedirectRecord;
use studioespresso\seofields\SeoFields;
Expand Down Expand Up @@ -61,9 +60,9 @@ public function handleNotFound(Request $request, Site $site)
if ($redirect) {
if (is_array($redirect)) {
$record = $redirect['record'];
$notFoundModel->redirect = $record->id;
$notFoundModel->redirect = $record->getAttribute('id');
} else {
$notFoundModel->redirect = $redirect->id;
$notFoundModel->redirect = $redirect->getAttribute('id');
}
$notFoundModel->handled = true;
}
Expand Down Expand Up @@ -93,10 +92,6 @@ public function markAsHandled(NotFoundRecord|int $record): void
return;
}

/**
* @param NotFoundModel $model
* @return RedirectModel|false
*/
private function getMatchingRedirect(NotFoundModel $model): RedirectRecord|array|bool
{
Craft::debug("Check if our 404 is matched to a redirect", SeoFields::class);
Expand Down Expand Up @@ -197,7 +192,7 @@ private function shouldWeCleanupRedirects()
$toDelete->limit($limit);
$toDelete->orderBy("dateCreated ASC");
foreach ($toDelete->all() as $record) {
$this->deletetById($record->id);
$this->deletetById($record->getAttribute('id'));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/services/SitemapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ private function _shouldRenderProducts($sitemapSettings)
/** @phpstan-ignore-next-line */
$productTypeService = new ProductTypes();
$site = Craft::$app->getSites()->getCurrentSite();
/** @phpstan-ignore-next-line */
foreach ($productTypeService->getProductTypeSites($productType) as $productTypeSite) {
if ($productTypeSite->siteId == $site->id && $productTypeSite->hasUrls) {
return true;
Expand Down

0 comments on commit 05c7d07

Please sign in to comment.