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 Apr 5, 2024
1 parent 35be8b5 commit 1104788
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 50 deletions.
36 changes: 16 additions & 20 deletions src/Twigpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Craft;
use craft\base\Plugin;
use craft\events\DeleteTemplateCachesEvent;

use craft\events\PluginEvent;
use craft\events\RegisterCacheOptionsEvent;
use craft\events\TemplateEvent;
Expand All @@ -27,7 +26,6 @@
use nystudio107\twigpack\models\Settings;
use nystudio107\twigpack\services\Manifest as ManifestService;
use nystudio107\twigpack\variables\ManifestVariable;

use yii\base\Event;
use yii\web\NotFoundHttpException;

Expand All @@ -53,41 +51,38 @@ class Twigpack extends Plugin
/**
* @var string
*/
public static $templateName;
public static $templateName = '';

// Static Methods
// =========================================================================

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'manifest' => ManifestService::class,
];

parent::__construct($id, $parent, $config);
}

// Public Properties
// =========================================================================

/**
* @var string
*/
public $schemaVersion = '1.0.0';

// Public Properties
// =========================================================================
/**
* @var bool
*/
public $hasCpSection = false;

/**
* @var bool
*/
public $hasCpSettings = false;

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'manifest' => ManifestService::class,
];

parent::__construct($id, $parent, $config);
}

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -127,6 +122,7 @@ public function clearAllCaches()
public function injectErrorEntry()
{
if (Craft::$app->getResponse()->isServerError || Craft::$app->getResponse()->isClientError) {
/** @var Settings $settings */
$settings = self::$plugin->getSettings();
if (!empty($settings->errorEntry) && $settings->useDevServer) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function getCssInlineTags(string $path, array $attributes = []): s
public static function getCriticalCssTags(array $config, $name = null, array $attributes = []): string
{
// Resolve the template name
$template = Craft::$app->getView()->resolveTemplate($name ?? Twigpack::$templateName ?? '');
$template = Craft::$app->getView()->resolveTemplate($name ?? Twigpack::$templateName);
if ($template) {
$name = self::combinePaths(
pathinfo($template, PATHINFO_DIRNAME),
Expand Down Expand Up @@ -473,7 +473,7 @@ public static function getFileFromManifest(array $config, string $fileName, stri
}
try {
if (is_file($localPath)) {
return self::getFile($localPath) ?? '';
return self::getFile($localPath);
}
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
Expand Down Expand Up @@ -653,7 +653,7 @@ function() use ($path, $callback) {
*/
protected static function getHttpResponseCode($url, $context)
{
$headers = @get_headers($url, 0, $context);
$headers = @get_headers($url, false, $context);
if (empty($headers)) {
return '404';
}
Expand Down
15 changes: 7 additions & 8 deletions src/services/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use craft\base\Component;
use nystudio107\twigpack\helpers\Manifest as ManifestHelper;

use nystudio107\twigpack\Twigpack;

use Twig\Error\LoaderError;
use yii\web\NotFoundHttpException;

/** @noinspection MissingPropertyAnnotationsInspection */
Expand Down Expand Up @@ -68,7 +67,7 @@ public function getCssInlineTags(string $path, array $attributes = []): string
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
*
* @return string
* @throws \Twig\Error\LoaderError
* @throws LoaderError
*/
public function getCriticalCssTags($name = null, $config = null, array $attributes = []): string
{
Expand All @@ -92,8 +91,8 @@ public function getCssRelPreloadPolyfill(): string
/**
* Return the HTML tags to include the JavaScript module
*
* @param string $moduleName
* @param bool $async
* @param string $moduleName
* @param bool $async
* @param null|array $config
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
*
Expand Down Expand Up @@ -125,7 +124,7 @@ public function getSafariNomoduleFix(array $attributes = []): string
*
* @param string $moduleName
* @param string $type
* @param null $config
* @param ?array $config
*
* @return null|string
* @throws NotFoundHttpException
Expand All @@ -143,7 +142,7 @@ public function getModule(string $moduleName, string $type = 'modern', $config =
*
* @param string $moduleName
* @param string $type
* @param null $config
* @param ?array $config
*
* @return null|string
* @throws NotFoundHttpException
Expand Down Expand Up @@ -173,7 +172,7 @@ public function getFile(string $path): string
*
* @param string $fileName
* @param string $type
* @param null $config
* @param ?array $config
*
* @return string
*/
Expand Down
36 changes: 17 additions & 19 deletions src/variables/ManifestVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
namespace nystudio107\twigpack\variables;

use craft\helpers\Template;

use nystudio107\twigpack\Twigpack;

use Twig\Error\LoaderError;
use Twig\Markup;

use yii\web\NotFoundHttpException;

/**
Expand All @@ -38,7 +36,7 @@ class ManifestVariable
public static function includeCssRelPreloadPolyfill(): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getCssRelPreloadPolyfill() ?? ''
Twigpack::$plugin->manifest->getCssRelPreloadPolyfill()
);
}

Expand All @@ -53,7 +51,7 @@ public static function includeCssRelPreloadPolyfill(): Markup
public function includeCssModule(string $moduleName, bool $async = false, array $attributes = []): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getCssModuleTags($moduleName, $async, null, $attributes) ?? ''
Twigpack::$plugin->manifest->getCssModuleTags($moduleName, $async, null, $attributes)
);
}

Expand All @@ -68,7 +66,7 @@ public function includeCssModule(string $moduleName, bool $async = false, array
public function includeInlineCssTags(string $path, array $attributes = []): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getCssInlineTags($path, $attributes) ?? ''
Twigpack::$plugin->manifest->getCssInlineTags($path, $attributes)
);
}

Expand All @@ -80,18 +78,18 @@ public function includeInlineCssTags(string $path, array $attributes = []): Mark
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
*
* @return Markup
* @throws \Twig\Error\LoaderError
* @throws LoaderError
*/
public function includeCriticalCssTags($name = null, array $attributes = []): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes) ?? ''
Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes)
);
}

/**
* @param string $moduleName
* @param bool $async
* @param string $moduleName
* @param bool $async
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag
*
* @return null|Markup
Expand All @@ -100,7 +98,7 @@ public function includeCriticalCssTags($name = null, array $attributes = []): Ma
public function includeJsModule(string $moduleName, bool $async = false, array $attributes = [])
{
return Template::raw(
Twigpack::$plugin->manifest->getJsModuleTags($moduleName, $async, null, $attributes) ?? ''
Twigpack::$plugin->manifest->getJsModuleTags($moduleName, $async, null, $attributes)
);
}

Expand All @@ -109,15 +107,15 @@ public function includeJsModule(string $moduleName, bool $async = false, array $
*
* @param string $moduleName
* @param string $type
* @param null $config
* @param null $config
*
* @return null|Markup
* @throws NotFoundHttpException
*/
public function getModuleUri(string $moduleName, string $type = 'modern', $config = null)
{
return Template::raw(
Twigpack::$plugin->manifest->getModule($moduleName, $type, $config) ?? ''
Twigpack::$plugin->manifest->getModule($moduleName, $type, $config)
);
}

Expand All @@ -126,15 +124,15 @@ public function getModuleUri(string $moduleName, string $type = 'modern', $confi
*
* @param string $moduleName
* @param string $type
* @param null $config
* @param null $config
*
* @return null|Markup
* @throws NotFoundHttpException
*/
public function getModuleHash(string $moduleName, string $type = 'modern', $config = null)
{
return Template::raw(
Twigpack::$plugin->manifest->getModuleHash($moduleName, $type, $config) ?? ''
Twigpack::$plugin->manifest->getModuleHash($moduleName, $type, $config)
);
}

Expand All @@ -148,7 +146,7 @@ public function getModuleHash(string $moduleName, string $type = 'modern', $conf
public function includeSafariNomoduleFix(array $attributes = []): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getSafariNomoduleFix($attributes) ?? ''
Twigpack::$plugin->manifest->getSafariNomoduleFix($attributes)
);
}

Expand All @@ -162,7 +160,7 @@ public function includeSafariNomoduleFix(array $attributes = []): Markup
public function includeFile(string $path): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getFile($path) ?? ''
Twigpack::$plugin->manifest->getFile($path)
);
}

Expand All @@ -171,14 +169,14 @@ public function includeFile(string $path): Markup
*
* @param string $fileName
* @param string $type
* @param null $config
* @param null $config
*
* @return Markup
*/
public function includeFileFromManifest(string $fileName, string $type = 'legacy', $config = null): Markup
{
return Template::raw(
Twigpack::$plugin->manifest->getFileFromManifest($fileName, $type, $config) ?? ''
Twigpack::$plugin->manifest->getFileFromManifest($fileName, $type, $config)
);
}
}

0 comments on commit 1104788

Please sign in to comment.