Skip to content

Commit

Permalink
ECS & PHPstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Feb 8, 2024
1 parent c701d07 commit f8fee80
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 83 deletions.
18 changes: 2 additions & 16 deletions src/SeoFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use craft\services\UserPermissions;
use craft\utilities\ClearCaches;
use craft\web\ErrorHandler;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use studioespresso\seofields\behaviors\EntrySeoBehavior;
use studioespresso\seofields\events\RegisterSeoElementEvent;
Expand All @@ -47,7 +46,6 @@
use studioespresso\seofields\services\RenderService;
use studioespresso\seofields\services\SchemaService;
use studioespresso\seofields\services\SitemapService;
use studioespresso\seofields\variables\SeoFieldsVariable;
use yii\base\Event;
use yii\base\Exception;
use yii\console\Application as ConsoleApplication;
Expand Down Expand Up @@ -122,7 +120,6 @@ public function init()
$this->_registerSiteListeners();
$this->_registerCacheOptions();
$this->_registerCustomElements();
$this->_registerTwigVariable();
$this->_registerUrlChangeListeners();

Event::on(Entry::class, Entry::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) {
Expand Down Expand Up @@ -454,9 +451,11 @@ private function _registerCustomElements()
{
$elements = [];
if (Craft::$app->getPlugins()->isPluginEnabled('calendar')) {
/** @phpstan-ignore-next-line */
$elements[] = \Solspace\Calendar\Elements\Event::class;
}
if (Craft::$app->getPlugins()->isPluginEnabled('commerce')) {
/** @phpstan-ignore-next-line */
$elements[] = \craft\commerce\elements\Product::class;
}

Expand All @@ -468,17 +467,4 @@ function(RegisterSeoElementEvent $event) use ($elements) {
);
}
}

private function _registerTwigVariable()
{
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('schema', SeoFieldsVariable::class);
}
);
}
}
1 change: 1 addition & 0 deletions src/console/controllers/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function options($actionId)
case 'fields':
return ['fieldHandle', 'metaTitle', 'metaDescription'];
}
return parent::options($actionId);
}

public function actionEther()
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/RedirectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function getHeaders($reader)
{
// Support for league/csv v8 with a header
try {
return $csv->fetchOne(0);
return $reader->fetchOne(0);
} catch (\Throwable $e) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/jobs/MigrateFieldDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MigrateFieldDataJob extends BaseJob
public $metaDescription;


public function init()
public function init(): void
{
if (!$this->fieldHandle) {
throw new InvalidFieldException('Field handle not provided');
Expand All @@ -26,7 +26,7 @@ public function init()
}


public function execute($queue)
public function execute($queue): void
{
$model = new SeoFieldModel();
if ($this->entry->metaTitle) {
Expand Down
4 changes: 2 additions & 2 deletions src/models/SeoFieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getSchema(Element $element = null)

/** @var $schema Schema */
$schema = \Craft::createObject($schemaClass);
$schema->name($this->getMetaTitle($element, false) ?? "");
$schema->name($this->getMetaTitle($element) ?? "");
$schema->description($this->getMetaDescription() ?? "");
$schema->url($element->getUrl() ?? "");
break;
Expand All @@ -99,7 +99,7 @@ public function getSchema(Element $element = null)

/** @var $schema Schema */
$schema = Craft::createObject($schemaClass);
$schema->name($this->getMetaTitle($element, false) ?? "");
$schema->name($this->getMetaTitle($element) ?? "");
$schema->description($this->getMetaDescription() ?? "");
$schema->url($element->getUrl() ?? "");
break;
Expand Down
23 changes: 0 additions & 23 deletions src/services/NotFoundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,6 @@ public function handleNotFoundException()
$this->handleNotFound($request, $site);
}

public function getAllNotFound($orderBy, $siteHandle = null, $handled)
{
$data = [];
$query = NotFoundRecord::find();
$query->orderBy("$orderBy DESC, dateLastHIT DESC");
$query->where(['in', 'siteId', Craft::$app->getSites()->getEditableSiteIds()]);
if ($siteHandle) {
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
$query->andWhere(['siteId' => $site->id]);
}

if ($handled !== "all") {
$query->andWhere(Db::parseParam('handled', $handled));
}

foreach ($query->all() as $record) {
$model = new NotFoundModel();
$model->setAttributes($record->getAttributes());
$data[] = $model;
}
return $data;
}

public function handleNotFound(Request $request, Site $site)
{
try {
Expand Down
38 changes: 0 additions & 38 deletions src/services/SchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace studioespresso\seofields\services;

use craft\base\Component;
use craft\base\Element;
use craft\elements\Category;
use craft\elements\Entry;
use Spatie\SchemaOrg\Schema;
use studioespresso\seofields\SeoFields;

Expand Down Expand Up @@ -34,39 +31,4 @@ public function schema()
{
return new Schema();
}

public function getSchemaForElement(Element $element): Schema|null
{
try {
$settings = $this->siteDefault->getSchema();
switch (get_class($element)) {
case Entry::class:
$schemaSettings = $settings['sections'];
$sectionId = $element->section->id;
$schemaClass = $schemaSettings[$sectionId];

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

/** @var $schema Schema */
$schema = Craft::createObject($schemaClass);
$schema->name($this->getMetaTitle($element, false) ?? "");
$schema->description($this->getMetaDescription() ?? "");
$schema->url($element->getUrl() ?? "");
break;
}
return $schema;
} catch (\Exception $e) {
\Craft::error($e, SeoFields::class);
return null;
}
}
}
6 changes: 5 additions & 1 deletion src/services/SitemapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function getSitemapData($siteId, $type, $sectionId)
$data = [];
switch ($type) {
case 'product':
$data = Product::findAll([
/** @phpstan-ignore-next-line */
$data = Product::findAll([
'siteId' => $siteId,
'typeId' => $sectionId,
'orderBy' => 'dateUpdated DESC',
Expand Down Expand Up @@ -279,7 +280,9 @@ private function _addProductsToIndex($productTypes, $site)
{
$data = [];
foreach ($productTypes as $id => $settings) {
/** @phpstan-ignore-next-line */
$type = Commerce::getInstance()->productTypes->getProductTypeById($id);
/** @phpstan-ignore-next-line */
$entry = Product::findOne(['typeId' => $type->id, 'orderBy' => 'dateUpdated DESC']);
if ($entry) {
$data[] = implode('', $this->_addItemToIndex($site, $type, $entry));
Expand Down Expand Up @@ -346,6 +349,7 @@ private function _shouldRenderProducts($sitemapSettings)

$shouldRenderProducts = array_filter($sitemapSettings['product'], function($productType) use ($sitemapSettings) {
if (isset($sitemapSettings['product'][$productType]['enabled'])) {
/** @phpstan-ignore-next-line */
$productTypeService = new ProductTypes();
$site = Craft::$app->getSites()->getCurrentSite();
foreach ($productTypeService->getProductTypeSites($productType) as $productTypeSite) {
Expand Down
1 change: 1 addition & 0 deletions src/services/migrate/Ether.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private function migrateContent(Entry $entry, $field, $newHandle)
}
}

/** @phpstan-ignore-next-line */
private function getTitle(SeoData $data)
{
if (count($data->titleRaw)) {
Expand Down

0 comments on commit f8fee80

Please sign in to comment.