Skip to content

Commit

Permalink
refactor: PHPStan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 18, 2024
1 parent 74d1b37 commit 4c32cf7
Show file tree
Hide file tree
Showing 68 changed files with 801 additions and 877 deletions.
12 changes: 7 additions & 5 deletions src/Seomatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use craft\base\Model;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\elements\User;
use craft\errors\SiteNotFoundException;
use craft\events\DefineGqlTypeFieldsEvent;
use craft\events\ElementEvent;
Expand Down Expand Up @@ -258,6 +257,7 @@ public function init(): void
public function getSettings(): ?Model
{
// For all the emojis
/* @var Settings $settingsModel */
$settingsModel = parent::getSettings();
if ($settingsModel !== null && !self::$savingSettings) {
$attributes = $settingsModel->attributes();
Expand Down Expand Up @@ -314,9 +314,12 @@ public function clearAllCaches(): void
$gql->invalidateCaches();
}
// If the FastCGI Cache Bust plugin is installed, clear its caches too
/** @var ?FastcgiCacheBust $plugin */
$plugin = Craft::$app->getPlugins()->getPlugin('fastcgi-cache-bust');
if ($plugin !== null) {
FastcgiCacheBust::$plugin->cache->clearAll();
// FastcgiCacheBust has an error in its PHPdoc
/** @phpstan-ignore-next-line */
$plugin->cache->clearAll();
}
}

Expand All @@ -339,7 +342,6 @@ public function getCpNavItem(): ?array
{
$subNavs = [];
$navItem = parent::getCpNavItem();
/** @var User $currentUser */
$request = Craft::$app->getRequest();
$siteSuffix = '';
if ($request->getSegment(1) === 'seomatic') {
Expand Down Expand Up @@ -526,7 +528,7 @@ static function(ModelEvent $event) {
'Element::EVENT_AFTER_PROPAGATE',
__METHOD__
);
/** @var $element Element */
/** @var Element $element */
$element = $event->sender;
self::$plugin->metaBundles->invalidateMetaBundleByElement(
$element,
Expand All @@ -546,7 +548,7 @@ static function(ElementEvent $event) {
'Elements::EVENT_AFTER_DELETE_ELEMENT',
__METHOD__
);
/** @var $element Element */
/** @var Element $element */
$element = $event->element;
self::$plugin->metaBundles->invalidateMetaBundleByElement(
$element,
Expand Down
2 changes: 1 addition & 1 deletion src/autocompletes/TrackingVarsAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TrackingVarsAutocomplete extends Autocomplete
public $type = AutocompleteTypes::TwigExpressionAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = false;

Expand Down
3 changes: 1 addition & 2 deletions src/base/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ abstract class Container extends FluentModel implements ContainerInterface
*
* @param array $config
*
* @return null|Container
* @return static
*/
public static function create(array $config = [])
{
$className = static::class;
/** @var Container $model */
$model = new $className($config);
$model->normalizeContainerData();

Expand Down
24 changes: 7 additions & 17 deletions src/base/FluentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

namespace nystudio107\seomatic\base;

use Craft;
use craft\base\Model;

use twig\Markup;

use ReflectionClass;
use Twig\Markup;
use yii\base\InvalidArgumentException;
use function is_object;

/**
* @author nystudio107
Expand All @@ -32,7 +31,7 @@ abstract class FluentModel extends Model
* Remove any properties that don't exist in the model
*
* @param string $class
* @param array $config
* @param array $config
*/
protected static function cleanProperties(string $class, array &$config)
{
Expand All @@ -50,22 +49,13 @@ protected static function cleanProperties(string $class, array &$config)
* Add fluent getters/setters for this class
*
* @param string $method The method name (property name)
* @param array $args The arguments list
* @param array $args The arguments list
*
* @return mixed The value of the property
*/
public function __call($method, $args)
{
try {
$reflector = new \ReflectionClass(static::class);
} catch (\ReflectionException $e) {
Craft::error(
$e->getMessage(),
__METHOD__
);

return null;
}
$reflector = new ReflectionClass(static::class);
if (!$reflector->hasProperty($method)) {
throw new InvalidArgumentException("Property {$method} doesn't exist");
}
Expand All @@ -76,7 +66,7 @@ public function __call($method, $args)
}
// Set the property
$value = $args[0];
if (\is_object($value) && $value instanceof Markup) {
if (is_object($value) && $value instanceof Markup) {
$value = (string)$value;
}
$property->setValue($this, $value);
Expand Down
1 change: 0 additions & 1 deletion src/base/MetaContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function renderArray(array $params = []): array
$htmlArray = [];

if ($this->prepForInclusion()) {
/** @var $metaItemModel MetaItem */
foreach ($this->data as $metaItemModel) {
if ($metaItemModel->include) {
$htmlArray[$metaItemModel->key] = $metaItemModel->renderAttributes($params);
Expand Down
32 changes: 18 additions & 14 deletions src/base/MetaItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
namespace nystudio107\seomatic\base;

use Craft;
use Exception;
use nystudio107\seomatic\behaviors\MetaItemAttributeParserBehavior;
use nystudio107\seomatic\helpers\ArrayHelper;
use nystudio107\seomatic\helpers\Dependency;
use nystudio107\seomatic\models\MetaJsonLd;
use nystudio107\seomatic\Seomatic;
use yii\helpers\Inflector;
use function count;
use function get_class;
use function is_array;
use function is_string;

/**
* @author nystudio107
Expand Down Expand Up @@ -52,9 +57,9 @@ public function init(): void
$envVars = null;
try {
$envVars = ArrayHelper::getValue($this->environment, Seomatic::$environment);
} catch (\Exception $e) {
} catch (Exception $e) {
}
if (\is_array($envVars)) {
if (is_array($envVars)) {
foreach ($envVars as $key => $value) {
$attributes[$key] = $value;
}
Expand Down Expand Up @@ -155,7 +160,7 @@ public function debugMetaItem(
$extraInfo = '';
// Add a URL to the schema.org type if this is a MetaJsonLD object
if ($isMetaJsonLdModel) {
/** @var $this MetaJsonLd */
/** @var MetaJsonLd $this */
$extraInfo = ' for http://schema.org/' . $this->type;
}
$errorMsg =
Expand Down Expand Up @@ -187,11 +192,10 @@ public function debugMetaItem(
// Extra debugging info for MetaJsonLd objects
if ($isMetaJsonLdModel) {
/** @var MetaJsonLd $className */
$className = \get_class($this);
if (!empty($className::$schemaPropertyDescriptions[$param])) {
$className = get_class($this);
if (!empty($className->schemaPropertyDescriptions[$param])) {
$errorMsg = Craft::t('seomatic', $errorLabel) . $param;
/** @var $className MetaJsonLd */
$errorMsg .= ' -> ' . $className::$schemaPropertyDescriptions[$param];
$errorMsg .= ' -> ' . $className->schemaPropertyDescriptions[$param];
Craft::info($errorMsg, __METHOD__);
}
}
Expand All @@ -207,7 +211,7 @@ public function debugMetaItem(
*/
public function tagAttributes(): array
{
$attrs = $this->tagAttrs ?? [];
$attrs = $this->tagAttrs;
if (!is_array($attrs)) {
$attrs = [];
}
Expand Down Expand Up @@ -243,16 +247,16 @@ public function tagAttributesArray(): array

// See if any of the potentially array properties actually are
foreach (static::ARRAY_PROPERTIES as $arrayProperty) {
if (!empty($options[$arrayProperty]) && \is_array($options[$arrayProperty])) {
$optionsCount = \count($options[$arrayProperty]) > $optionsCount
? \count($options[$arrayProperty]) : $optionsCount;
if (!empty($options[$arrayProperty]) && is_array($options[$arrayProperty])) {
$optionsCount = count($options[$arrayProperty]) > $optionsCount
? count($options[$arrayProperty]) : $optionsCount;
}
}
// Return an array of resulting options
while ($optionsCount--) {
$resultOptions = $options;
foreach ($resultOptions as $key => $value) {
$resultOptions[$key] = (\is_array($value) && isset($value[$optionsCount]))
$resultOptions[$key] = (is_array($value) && isset($value[$optionsCount]))
? $value[$optionsCount] : $value;
}
$result[] = $resultOptions;
Expand All @@ -272,10 +276,10 @@ public function validateStringOrArray(
$params,
) {
$validated = false;
if (\is_string($attribute)) {
if (is_string($attribute)) {
$validated = true;
}
if (\is_array($attribute)) {
if (is_array($attribute)) {
$validated = true;
}
if (!$validated) {
Expand Down
9 changes: 7 additions & 2 deletions src/base/MetaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use craft\base\Component;

use nystudio107\seomatic\Seomatic;

/**
* @author nystudio107
* @package Seomatic
Expand All @@ -35,33 +33,40 @@ abstract class MetaService extends Component implements MetaServiceInterface
*/
public function get(string $key, string $handle = '')
{
return null;
}

/**
* @inheritdoc
*/
public function create(array $config = [], $add = true)
{
// The non-abstract classes always return a MetaItem
/** @phpstan-ignore-next-line */
return null;
}

/**
* @inheritdoc
*/
public function add($metaItem, string $handle = '')
{
return null;
}

/**
* @inheritdoc
*/
public function render()
{
return null;
}

/**
* @inheritdoc
*/
public function container(string $handle = '')
{
return null;
}
}
7 changes: 4 additions & 3 deletions src/controllers/ContentSeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use nystudio107\seomatic\Seomatic;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use function count;

/**
* @author nystudio107
Expand All @@ -42,7 +43,7 @@ class ContentSeoController extends Controller
// =========================================================================

/**
* @var bool|array
* @inheritdoc
*/
protected array|bool|int $allowAnonymous = [
];
Expand Down Expand Up @@ -152,8 +153,8 @@ public function actionMetaBundles(
$dataItem['robots'] = $metaBundle->metaGlobalVars->robots;
// Calculate the setup stat
$stat = 0;
$numGrades = \count(SettingsController::SETUP_GRADES);
$numFields = \count(SettingsController::SEO_SETUP_FIELDS);
$numGrades = count(SettingsController::SETUP_GRADES);
$numFields = count(SettingsController::SEO_SETUP_FIELDS);
foreach (SettingsController::SEO_SETUP_FIELDS as $setupField => $setupLabel) {
$stat += (int)!empty($metaBundle->metaGlobalVars[$setupField]);
$value = $variables['contentSetupChecklist'][$setupField]['value'] ?? 0;
Expand Down
Loading

0 comments on commit 4c32cf7

Please sign in to comment.