Skip to content

Commit

Permalink
Adds phpstan cleanup core level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
BenParizek committed Nov 12, 2023
1 parent a285358 commit 1c354db
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/core/Sprout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace BarrelStrength\Sprout\core;

use BarrelStrength\Sprout\core\db\MigrationInterface;
use BarrelStrength\Sprout\core\db\MigrationTrait;
use BarrelStrength\Sprout\core\helpers\ConditionHelper;
use BarrelStrength\Sprout\core\helpers\PhoneHelper;
use BarrelStrength\Sprout\core\modules\CpNavHelper;
use BarrelStrength\Sprout\core\modules\Modules as ModulesService;
use BarrelStrength\Sprout\core\modules\Settings as SettingsService;
use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\TranslatableTrait;
use BarrelStrength\Sprout\core\twig\SproutExtension;
Expand Down Expand Up @@ -38,7 +40,7 @@
* @property ModulesService $coreModules
* @property VitePluginService $vite
*/
class Sprout extends Module
class Sprout extends Module implements SproutModuleInterface, MigrationInterface
{
use SproutModuleTrait;
use MigrationTrait;
Expand Down
7 changes: 4 additions & 3 deletions src/core/SproutSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BarrelStrength\Sprout\core;

use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use Craft;
use craft\config\BaseConfig;
Expand Down Expand Up @@ -56,10 +57,10 @@ public function getCpSettingsRows(): array
];
}

uksort($cpSettingsRows, static function($a, $b): int {
uksort($cpSettingsRows, static function($a, $b) {
/**
* @var $a SproutModuleTrait
* @var $b SproutModuleTrait
* @var SproutModuleInterface $a
* @var SproutModuleInterface $b
*/
return $a::getDisplayName() <=> $b::getDisplayName();
});
Expand Down
4 changes: 2 additions & 2 deletions src/core/controllers/web/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BarrelStrength\Sprout\core\controllers\web;

use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use Craft;
use craft\web\Controller;
use craft\web\twig\variables\Cp as CpVariable;
Expand All @@ -25,7 +25,7 @@ public function actionSaveSettings(): ?Response
$moduleId = Craft::$app->getRequest()->getBodyParam('moduleId');
$settings = Craft::$app->getRequest()->getBodyParam('settings');

/** @var SproutModuleTrait $module */
/** @var SproutModuleInterface $module */
$module = Craft::$app->getModule($moduleId);

if (!$module) {
Expand Down
13 changes: 9 additions & 4 deletions src/core/db/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BarrelStrength\Sprout\core\db;

use BarrelStrength\Sprout\core\migrations\Uninstall;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\Sprout;
use BarrelStrength\Sprout\core\SproutSettings;
use Craft;
Expand Down Expand Up @@ -93,14 +93,19 @@ public static function runUninstallMigrations(SproutPluginMigrationInterface $pl

$modulesSafeToUninstall = array_diff($plugin::getSchemaDependencies(), $requiredModules);

/** @var SproutModuleTrait $moduleClass */
/** @var SproutModuleInterface $moduleClass */
foreach ($modulesSafeToUninstall as $moduleClass) {
if (!$moduleClass::hasMigrations()) {
continue;
}

/** @var MigrationManager $migrator */
$migrator = $moduleClass::getInstance()->getMigrator();
$module = $moduleClass::getInstance();

if (!$module instanceof MigrationInterface) {
continue;
}

$migrator = $module->getMigrator();

if (($migration = self::createUninstallMigration($migrator)) !== null) {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/core/db/MigrationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BarrelStrength\Sprout\core\db;

use craft\base\Plugin;
use craft\db\MigrationManager;

interface MigrationInterface
{
public function getMigrator(): MigrationManager;
}
3 changes: 3 additions & 0 deletions src/core/db/SproutPluginMigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace BarrelStrength\Sprout\core\db;

use craft\base\Plugin;
use craft\db\MigrationManager;

/**
* Sprout Plugins using modules with schema must implement this interface
*
* @mixin Plugin
*/
interface SproutPluginMigrationInterface
{
Expand Down
18 changes: 13 additions & 5 deletions src/core/db/SproutPluginMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace BarrelStrength\Sprout\core\db;

use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use Craft;
use craft\base\Plugin;
use craft\db\MigrationManager;
use ReflectionClass;

Expand All @@ -13,7 +12,7 @@ class SproutPluginMigrator extends MigrationManager
/**
* Adds the schemaDependencies property to the MigrationManager class
*
* @var SproutModuleTrait[]|MigrationTrait[]
* @var SproutModuleInterface[]
*/
public array $schemaDependencies = [];

Expand All @@ -23,7 +22,7 @@ class SproutPluginMigrator extends MigrationManager
* Note: This migrator will never need to run any migrations for the
* plugin itself but is necessary to run Sprout module migrations
*/
public static function make(Plugin $plugin): SproutPluginMigrator
public static function make(SproutPluginMigrationInterface $plugin): SproutPluginMigrator
{
$ref = new ReflectionClass($plugin);
$ns = $ref->getNamespaceName();
Expand All @@ -49,14 +48,23 @@ public function up(int $limit = 0): void
parent::up();

// Loop through Sprout modules
/** @var SproutModuleInterface $moduleClass */
foreach ($this->schemaDependencies as $moduleClass) {
if (!$moduleClass::hasMigrations()) {
continue;
}

ob_start();
$migrator = $moduleClass::getInstance()->getMigrator();

$module = $moduleClass::getInstance();

if (!$module instanceof MigrationInterface) {
continue;
}

$migrator = $module->getMigrator();
$migrator->up();

ob_end_clean();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/modules/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ class SettingsHelper
* ideally we can ditch this method and migrate to use a native
* Craft method if/when they better support third-party modules
*/
public static function getSettingsConfig(Module $module, string $settingsClass): BaseConfig
public static function getSettingsConfig(SproutModuleInterface $module, string $settingsClass): BaseConfig
{
$configClass = $settingsClass;

/** @var SproutModuleTrait $module */
$envPrefix = $module::getEnvPrefix();

$projectConfigService = Craft::$app->getProjectConfig();
Expand Down Expand Up @@ -97,6 +96,7 @@ public static function saveDbSettings($moduleId, $settings, $siteId): SettingsRe
$settingsRecord = null;
try {
foreach ($settings as $name => $setting) {
/** @var SettingsRecord $settingsRecord */
$settingsRecord = SettingsRecord::find()
->select('*')
->where([
Expand Down
32 changes: 32 additions & 0 deletions src/core/modules/SproutModuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BarrelStrength\Sprout\core\modules;

use BarrelStrength\Sprout\datastudio\components\elements\DataSetElement;
use craft\base\SavableComponentInterface;
use craft\config\BaseConfig;
use yii\base\Module;

/**
* @mixin Module
*/
interface SproutModuleInterface
{
public static function isEnabled(): bool;

public static function hasEditions(): bool;

public static function hasMigrations(): bool;

public static function getModuleId(): string;

public static function getDisplayName(): string;

public static function getShortName(): string;

public function createSettingsModel(): ?BaseConfig;

public static function projectConfigPath(string $path = null): string;

public static function getEnvPrefix(): string;
}
7 changes: 7 additions & 0 deletions src/core/modules/SproutModuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use BarrelStrength\Sprout\core\Sprout;
use BarrelStrength\Sprout\core\SproutSettings;
use Craft;
use craft\config\BaseConfig;
use craft\helpers\StringHelper;
use yii\base\Module;

trait SproutModuleTrait
{
Expand Down Expand Up @@ -73,6 +75,11 @@ public static function getEnvPrefix(): string
return strtoupper(StringHelper::toSnakeCase(static::getModuleId())) . '_';
}

public function createSettingsModel(): ?BaseConfig
{
return null;
}

/**
* Returns the project config path
*
Expand Down
3 changes: 2 additions & 1 deletion src/core/twig/SproutVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BarrelStrength\Sprout\core\twig;

use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\Sprout;
use BarrelStrength\Sprout\datastudio\datasets\TwigDataSetVariable;
use BarrelStrength\Sprout\datastudio\DataStudioModule;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function init(): void
* Example:
* sprout.modules.[moduleShortName]
*/
public function registerModule(Module $module): void
public function registerModule(SproutModuleInterface $module): void
{
$this->modules[StringHelper::toCamelCase($module::getShortName())] = $module;
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/twig/TemplateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BarrelStrength\Sprout\core\twig;

use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\Sprout;
use BarrelStrength\Sprout\core\SproutSettings;
Expand Down Expand Up @@ -94,10 +95,10 @@ public static function optionsFromComponentTypes($savableComponentTypes, string
return $options;
}

public static function getConfigWarning(Module $module, string $setting): string
public static function getConfigWarning(SproutModuleInterface $sproutModule, string $setting): string
{
/** @var SproutModuleTrait $module */
$module = new $module($module::class);
$module = new $sproutModule($sproutModule::class);

$settings = $module->createSettingsModel();

$envConfig = App::envConfig($settings::class, $module::getEnvPrefix());
Expand Down
4 changes: 3 additions & 1 deletion src/datastudio/DataStudioModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace BarrelStrength\Sprout\datastudio;

use BarrelStrength\Sprout\core\db\MigrationInterface;
use BarrelStrength\Sprout\core\db\MigrationTrait;
use BarrelStrength\Sprout\core\editions\EditionTrait;
use BarrelStrength\Sprout\core\modules\CpNavHelper;
use BarrelStrength\Sprout\core\modules\Settings;
use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\TranslatableTrait;
use BarrelStrength\Sprout\core\Sprout;
Expand Down Expand Up @@ -44,7 +46,7 @@
* @property DataSources $dataSources
* @property Visualizations $visualizations
*/
class DataStudioModule extends Module
class DataStudioModule extends Module implements SproutModuleInterface, MigrationInterface
{
use SproutModuleTrait;
use EditionTrait;
Expand Down
4 changes: 3 additions & 1 deletion src/forms/FormsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace BarrelStrength\Sprout\forms;

use BarrelStrength\Sprout\core\db\MigrationInterface;
use BarrelStrength\Sprout\core\db\MigrationTrait;
use BarrelStrength\Sprout\core\editions\EditionTrait;
use BarrelStrength\Sprout\core\modules\Settings;
use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\TranslatableTrait;
use BarrelStrength\Sprout\core\relations\RelationsHelper;
Expand Down Expand Up @@ -68,7 +70,7 @@
* @property Addresses $addressField
* @property AddressFormatter $addressFormatter
*/
class FormsModule extends Module
class FormsModule extends Module implements SproutModuleInterface, MigrationInterface
{
use SproutModuleTrait;
use EditionTrait;
Expand Down
4 changes: 3 additions & 1 deletion src/mailer/MailerModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace BarrelStrength\Sprout\mailer;

use BarrelStrength\Sprout\core\db\MigrationInterface;
use BarrelStrength\Sprout\core\db\MigrationTrait;
use BarrelStrength\Sprout\core\modules\CpNavHelper;
use BarrelStrength\Sprout\core\modules\Settings;
use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\TranslatableTrait;
use BarrelStrength\Sprout\core\Sprout;
Expand Down Expand Up @@ -48,7 +50,7 @@
* @property EmailTypes $emailTypes
* @property SubscriberLists $subscriberLists
*/
class MailerModule extends Module
class MailerModule extends Module implements SproutModuleInterface, MigrationInterface
{
use SproutModuleTrait;
use MigrationTrait;
Expand Down
4 changes: 3 additions & 1 deletion src/meta/MetaModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace BarrelStrength\Sprout\meta;

use BarrelStrength\Sprout\core\db\MigrationInterface;
use BarrelStrength\Sprout\core\db\MigrationTrait;
use BarrelStrength\Sprout\core\editions\EditionTrait;
use BarrelStrength\Sprout\core\modules\CpNavHelper;
use BarrelStrength\Sprout\core\modules\Settings;
use BarrelStrength\Sprout\core\modules\SettingsHelper;
use BarrelStrength\Sprout\core\modules\SproutModuleInterface;
use BarrelStrength\Sprout\core\modules\SproutModuleTrait;
use BarrelStrength\Sprout\core\modules\TranslatableTrait;
use BarrelStrength\Sprout\core\Sprout;
Expand Down Expand Up @@ -41,7 +43,7 @@
* @property ElementMetadata $elementMetadata
* @property SchemaMetadata $schemaMetadata
*/
class MetaModule extends Module
class MetaModule extends Module implements SproutModuleInterface, MigrationInterface
{
use SproutModuleTrait;
use EditionTrait;
Expand Down
Loading

0 comments on commit 1c354db

Please sign in to comment.