Code styling /w Rector

This commit is contained in:
Belle Aerni 2023-01-25 22:08:19 -08:00
parent e1667b6374
commit af375a5499
11 changed files with 29 additions and 43 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector; use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector; use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector;
use Rector\Config\RectorConfig; use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector; use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Php80\Rector\FunctionLike\UnionTypesRector; use Rector\Php80\Rector\FunctionLike\UnionTypesRector;
use Rector\Set\ValueObject\SetList; use Rector\Set\ValueObject\SetList;
@ -21,6 +22,7 @@ return static function (RectorConfig $rectorConfig): void {
MixedTypeRector::class, MixedTypeRector::class,
EncapsedStringsToSprintfRector::class, EncapsedStringsToSprintfRector::class,
ConsistentPregDelimiterRector::class, ConsistentPregDelimiterRector::class,
RemoveAnnotationRector::class,
]); ]);
$rectorConfig->sets([ $rectorConfig->sets([
@ -28,5 +30,6 @@ return static function (RectorConfig $rectorConfig): void {
SetList::CODE_QUALITY, SetList::CODE_QUALITY,
SetList::CODING_STYLE, SetList::CODING_STYLE,
SetList::NAMING, SetList::NAMING,
SetList::DEAD_CODE,
]); ]);
}; };

View file

@ -73,7 +73,7 @@ class AntCMS
*/ */
public static function renderException(string $exceptionCode, int $httpCode = 404, string $exceptionString = 'That request caused an exception to be thrown.') public static function renderException(string $exceptionCode, int $httpCode = 404, string $exceptionString = 'That request caused an exception to be thrown.')
{ {
$exceptionString = $exceptionString . " (Code $exceptionCode)"; $exceptionString .= " (Code {$exceptionCode})";
$pageTemplate = self::getPageLayout(); $pageTemplate = self::getPageLayout();
$pageTemplate = str_replace('{{ AntCMSTitle }}', 'An error ocurred', $pageTemplate); $pageTemplate = str_replace('{{ AntCMSTitle }}', 'An error ocurred', $pageTemplate);
@ -84,8 +84,7 @@ class AntCMS
exit; exit;
} }
/** /**
* @param string $page
* @return array<mixed>|false * @return array<mixed>|false
*/ */
public function getPage(string $page) public function getPage(string $page)
@ -108,8 +107,7 @@ class AntCMS
} }
} }
/** /**
* @param string $layout
* @param string|null $theme * @param string|null $theme
* @return string * @return string
*/ */
@ -161,8 +159,7 @@ class AntCMS
return $template; return $template;
} }
/** /**
* @param string $pageContent
* @return array<mixed> * @return array<mixed>
*/ */
public static function getPageHeaders(string $pageContent) public static function getPageHeaders(string $pageContent)
@ -206,8 +203,7 @@ class AntCMS
return AntConfig::currentConfig('siteInfo'); return AntConfig::currentConfig('siteInfo');
} }
/** /**
* @param string $path
* @return void * @return void
*/ */
public function serveContent(string $path) public function serveContent(string $path)

View file

@ -21,7 +21,7 @@ class AntCache
$config = AntConfig::currentConfig(); $config = AntConfig::currentConfig();
if ($config['enableCache']) { if ($config['enableCache']) {
try { try {
file_put_contents($cachePath, (string)$content); file_put_contents($cachePath, $content);
return true; return true;
} catch (\Exception) { } catch (\Exception) {
return false; return false;

View file

@ -86,11 +86,12 @@ class AntConfig
*/ */
public static function saveConfig(array $config) public static function saveConfig(array $config)
{ {
foreach (self::$ConfigKeys as $defaultKey) { foreach (self::$ConfigKeys as $ConfigKey) {
if (!array_key_exists($defaultKey, $config)) { if (!array_key_exists($ConfigKey, $config)) {
throw new Exception("New config is missing the required {$defaultKey} key from it's array!"); throw new Exception("New config is missing the required {$ConfigKey} key from it's array!");
} }
} }
return AntYaml::saveFile(antConfigFile, $config); return AntYaml::saveFile(antConfigFile, $config);
} }
} }

View file

@ -17,8 +17,7 @@ use League\CommonMark\Extension\Embed\EmbedExtension;
class AntMarkdown class AntMarkdown
{ {
/** /**
* @param string $md
* @return string * @return string
*/ */
public static function renderMarkdown(string $md) public static function renderMarkdown(string $md)

View file

@ -59,7 +59,6 @@ class AntPages
} }
/** /**
* @param string $navTemplate
* @param string $currentPage optional - What page is the active page. Used for highlighting the active page in the navbar * @param string $currentPage optional - What page is the active page. Used for highlighting the active page in the navbar
* @return string * @return string
*/ */

View file

@ -10,7 +10,6 @@ class AntPluginLoader
public function loadPlugins() public function loadPlugins()
{ {
$plugins = array(); $plugins = array();
$files = array();
$files = AntTools::getFileList(antPluginPath, null, true); $files = AntTools::getFileList(antPluginPath, null, true);

View file

@ -4,10 +4,7 @@ namespace AntCMS;
class AntTools class AntTools
{ {
/** /**
* @param string $dir
* @param (null|string)|null $extension
* @param null|bool $returnPath
* @return array<string> * @return array<string>
*/ */
public static function getFileList(string $dir, ?string $extension = null, ?bool $returnPath = false) public static function getFileList(string $dir, ?string $extension = null, ?bool $returnPath = false)
@ -24,8 +21,7 @@ class AntTools
return $files; return $files;
} }
/** /**
* @param string $path
* @return string * @return string
*/ */
public static function repairFilePath(string $path) public static function repairFilePath(string $path)

View file

@ -6,8 +6,7 @@ use AntCMS\AntConfig;
class AntTwig class AntTwig
{ {
/** /**
* @param string $content
* @param array<mixed> $params * @param array<mixed> $params
* @param string|null $theme * @param string|null $theme
* @return string * @return string

View file

@ -7,8 +7,7 @@ use Symfony\Component\Yaml\Yaml;
class AntYaml class AntYaml
{ {
/** /**
* @param string $file
* @return array<mixed> * @return array<mixed>
*/ */
public static function parseFile(string $file) public static function parseFile(string $file)
@ -16,19 +15,16 @@ class AntYaml
return Yaml::parseFile($file); return Yaml::parseFile($file);
} }
/** /**
* @param string $file
* @param array<mixed> $data * @param array<mixed> $data
* @return bool
*/ */
public static function saveFile(string $file, array $data) public static function saveFile(string $file, array $data): bool
{ {
$yaml = Yaml::dump($data); $yaml = Yaml::dump($data);
return boolval(file_put_contents($file, $yaml)); return (bool) file_put_contents($file, $yaml);
} }
/** /**
* @param string $yaml
* @return array<mixed>|null * @return array<mixed>|null
*/ */
public static function parseYaml(string $yaml) public static function parseYaml(string $yaml)

View file

@ -11,7 +11,6 @@ use AntCMS\AntTwig;
class AdminPlugin extends AntPlugin class AdminPlugin extends AntPlugin
{ {
/** @return string */
public function getName(): string public function getName(): string
{ {
return 'Admin'; return 'Admin';
@ -158,6 +157,7 @@ class AdminPlugin extends AntPlugin
if (!str_ends_with($pagePath, ".md")) { if (!str_ends_with($pagePath, ".md")) {
$pagePath .= '.md'; $pagePath .= '.md';
} }
$pagePath = AntTools::repairFilePath($pagePath); $pagePath = AntTools::repairFilePath($pagePath);
$page = "--AntCMS--\nTitle: New Page Title\nAuthor: Author\nDescription: Description of this page.\nKeywords: Keywords\n--AntCMS--\n"; $page = "--AntCMS--\nTitle: New Page Title\nAuthor: Author\nDescription: Description of this page.\nKeywords: Keywords\n--AntCMS--\n";
} }
@ -200,12 +200,11 @@ class AdminPlugin extends AntPlugin
} }
} }
if (file_exists($pagePath)) { // If we were able to delete the page, update the pages list with the updated pages array.
// If we were able to delete the page, update the pages list with the updated pages array. if (file_exists($pagePath) && unlink($pagePath)) {
if (unlink($pagePath)) { AntYaml::saveFile(antPagesList, $pages);
AntYaml::saveFile(antPagesList, $pages);
}
} }
header('Location: //' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/"); header('Location: //' . AntConfig::currentConfig('baseURL') . "plugin/admin/pages/");
break; break;
@ -249,12 +248,11 @@ class AdminPlugin extends AntPlugin
exit; exit;
} }
/** /**
* @param bool $value
* @return string * @return string
*/ */
private function boolToWord(bool $value) private function boolToWord(bool $value)
{ {
return (bool) $value ? 'true' : 'false'; return $value ? 'true' : 'false';
} }
} }