diff --git a/phpstan.neon b/phpstan.neon index 2298851..24bb961 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 5 + level: 6 paths: - src excludePaths: diff --git a/src/AntCMS/AntAuth.php b/src/AntCMS/AntAuth.php index 6227325..a3aef52 100644 --- a/src/AntCMS/AntAuth.php +++ b/src/AntCMS/AntAuth.php @@ -6,6 +6,13 @@ use AntCMS\AntConfig; class AntAuth { + /** + * Check if the user is authenticated using the credentials in the config file. + * If the plain text password in the config file is still present, it will be hashed and the config file will be updated. + * If the user is not authenticated, it will call AntAuth::requireAuth() + * + * @return void + */ public static function checkAuth() { $currentConfig = AntConfig::currentConfig(); @@ -23,13 +30,18 @@ class AntAuth //Now, we can perform the check as normal if ($currentConfig['admin']['username'] == $_SERVER['PHP_AUTH_USER'] && password_verify($_SERVER['PHP_AUTH_PW'], $currentConfig['admin']['password'])) { - return true; + return; } } AntAuth::requireAuth(); } + /** + * Send an authentication challenge to the browser, with the realm set to the site title in config. + * + * @return void + */ private static function requireAuth() { $currentConfig = AntConfig::currentConfig(); diff --git a/src/AntCMS/AntCMS.php b/src/AntCMS/AntCMS.php index 673b0ac..294f2e9 100644 --- a/src/AntCMS/AntCMS.php +++ b/src/AntCMS/AntCMS.php @@ -9,7 +9,13 @@ use AntCMS\AntConfig; class AntCMS { - public function renderPage($page, $params = null) + /** + * Renders a page based on the provided page name. + * + * @param string $page The name of the page to be rendered + * @return string The rendered HTML of the page + */ + public function renderPage(string $page) { $start_time = microtime(true); $content = $this->getPage($page); @@ -42,7 +48,13 @@ class AntCMS return $pageTemplate; } - public function getPageLayout($theme = null) + /** + * Returns the default layout of the active theme unless otherwise specified. + * + * @param string|null $theme optional - the theme to get the page layout for. + * @return string the default page layout + */ + public function getPageLayout(string $theme = null) { $siteInfo = AntCMS::getSiteInfo(); @@ -55,7 +67,13 @@ class AntCMS return $pageTemplate; } - public function renderException($exceptionCode) + /** + * Render an exception page with the provided exception code. + * + * @param string $exceptionCode The exception code to be displayed on the error page + * @return never + */ + public function renderException(string $exceptionCode) { $pageTemplate = $this->getPageLayout(); @@ -66,7 +84,11 @@ class AntCMS exit; } - public function getPage($page) + /** + * @param string $page + * @return array|false + */ + public function getPage(string $page) { $page = strtolower($page); $pagePath = AntDir . "/Content/$page"; @@ -94,7 +116,12 @@ class AntCMS } } - public function getThemeTemplate($layout = 'default_layout', $theme = null) + /** + * @param string $layout + * @param string|null $theme + * @return string + */ + public function getThemeTemplate(string $layout = 'default_layout', string $theme = null) { $theme = $theme ?? AntConfig::currentConfig('activeTheme'); @@ -142,7 +169,11 @@ class AntCMS return $template; } - public static function getPageHeaders($pageContent) + /** + * @param string $pageContent + * @return array + */ + public static function getPageHeaders(string $pageContent) { $AntKeywords = new AntKeywords(); @@ -178,12 +209,19 @@ class AntCMS return $pageHeaders; } + /** + * @return mixed + */ public static function getSiteInfo() { return AntConfig::currentConfig('SiteInfo'); } - public function serveContent($path) + /** + * @param string $path + * @return void + */ + public function serveContent(string $path) { if (!file_exists($path)) { $this->renderException('404'); diff --git a/src/AntCMS/AntCache.php b/src/AntCMS/AntCache.php index 6b4644e..3a78e2b 100644 --- a/src/AntCMS/AntCache.php +++ b/src/AntCMS/AntCache.php @@ -10,12 +10,12 @@ class AntCache /** * Caches a value for a given cache key. * - * @param mixed $key The cache key to use for the cached value. - * @param mixed $content The value to cache. + * @param string $key The cache key to use for the cached value. + * @param string $content The value to cache. * @return bool True if the value was successfully cached, false otherwise. * @throws ParseException If there is an error parsing the AntCMS configuration file. */ - public function setCache($key, $content) + public function setCache(string $key, string $content) { $cachePath = AntCachePath . DIRECTORY_SEPARATOR . "$key.cache"; $config = AntConfig::currentConfig(); @@ -34,11 +34,11 @@ class AntCache /** * Retrieves the cached value for a given cache key. * - * @param mixed $key The cache key used to retrieve the cached value. + * @param string $key The cache key used to retrieve the cached value. * @return string|false The cached value, or false if there was an error loading it or if caching is disabled. * @throws ParseException If there is an error parsing the AntCMS configuration file. */ - public function getCache($key) + public function getCache(string $key) { $cachePath = AntCachePath . DIRECTORY_SEPARATOR . "$key.cache"; $config = AntConfig::currentConfig(); @@ -61,7 +61,7 @@ class AntCache * @return bool True if the cache key has a corresponding cached value, false otherwise. Will also return false if caching is disabled. * @throws ParseException If there is an error parsing the AntCMS configuration file. */ - public function isCached($key) + public function isCached(string $key) { $config = AntConfig::currentConfig(); if ($config['enableCache']) { @@ -76,11 +76,11 @@ class AntCache * Generates a unique cache key for the associated content and a salt value. * The salt is used to ensure that each cache key is unique to each component, even if multiple components are using the same source content but caching different results. * - * @param mixed $content The content to generate a cache key for. + * @param string $content The content to generate a cache key for. * @param string $salt An optional salt value to use in the cache key generation. Default is 'cache'. * @return string The generated cache key. */ - public function createCacheKey($content, $salt = 'cache') + public function createCacheKey(string $content, string $salt = 'cache') { /** * If the server is modern enough to have xxh128, use that. It is really fast and still produces long hashes diff --git a/src/AntCMS/AntConfig.php b/src/AntCMS/AntConfig.php index 490cb5e..e5b7488 100644 --- a/src/AntCMS/AntConfig.php +++ b/src/AntCMS/AntConfig.php @@ -37,7 +37,7 @@ class AntConfig * @param string|null $key The key of the configuration item to retrieve. Use dot notation to specify nested keys. * @return mixed The configuration array or a specific value if the key is specified. */ - public static function currentConfig($key = null) + public static function currentConfig(?string $key = null) { $config = AntYaml::parseFile(antConfigFile); if (is_null($key)) { @@ -49,7 +49,12 @@ class AntConfig } - private static function getArrayValue($array, $keys) + /** + * @param array $array + * @param array $keys + * @return mixed + */ + private static function getArrayValue(array $array, array $keys) { foreach ($keys as $k) { if (isset($array[$k])) { @@ -64,10 +69,10 @@ class AntConfig /** * Saves the AntCMS configuration * - * @param array $config The config data to be saved. + * @param array $config The config data to be saved. * @return void */ - public static function saveConfig($config) + public static function saveConfig(array $config) { AntYaml::saveFile(antConfigFile, $config); } diff --git a/src/AntCMS/AntKeywords.php b/src/AntCMS/AntKeywords.php index 72bd32c..bc82480 100644 --- a/src/AntCMS/AntKeywords.php +++ b/src/AntCMS/AntKeywords.php @@ -7,7 +7,12 @@ use AntCMS\AntConfig; class AntKeywords { - public function generateKeywords($content = '', $count = 15) + /** + * @param string $content + * @param int $count + * @return string + */ + public function generateKeywords(string $content = '', int $count = 15) { $cache = new AntCache(); $cacheKey = $cache->createCacheKey($content, 'keywords'); diff --git a/src/AntCMS/AntMarkdown.php b/src/AntCMS/AntMarkdown.php index b74b551..4870135 100644 --- a/src/AntCMS/AntMarkdown.php +++ b/src/AntCMS/AntMarkdown.php @@ -15,7 +15,11 @@ use ElGigi\CommonMarkEmoji\EmojiExtension; class AntMarkdown { - public static function renderMarkdown($md) + /** + * @param string $md + * @return string + */ + public static function renderMarkdown(string $md) { $cache = new AntCache(); $cacheKey = $cache->createCacheKey($md, 'markdown'); diff --git a/src/AntCMS/AntPages.php b/src/AntCMS/AntPages.php index 665975a..c547ed8 100644 --- a/src/AntCMS/AntPages.php +++ b/src/AntCMS/AntPages.php @@ -11,6 +11,7 @@ use AntCMS\AntTwig; class AntPages { + /** @return void */ public static function generatePages() { $pages = AntTools::getFileList(antContentPath, 'md', true); @@ -32,12 +33,17 @@ class AntPages AntYaml::saveFile(antPagesList, $pageList); } + /** @return array */ public static function getPages() { return AntYaml::parseFile(antPagesList); } - public static function generateNavigation($navTemplate = '') + /** + * @param string $navTemplate + * @return string + */ + public static function generateNavigation(string $navTemplate = '') { $pages = AntPages::getPages(); $cache = new AntCache; diff --git a/src/AntCMS/AntPlugin.php b/src/AntCMS/AntPlugin.php index bb1223c..5049af6 100644 --- a/src/AntCMS/AntPlugin.php +++ b/src/AntCMS/AntPlugin.php @@ -4,10 +4,15 @@ namespace AntCMS; abstract class AntPlugin { + /** + * @param array $route + * @return mixed + */ public function handlePluginRoute(array $route) { die("Plugin did not define a handlePluginRoute function"); } + /** @return string */ abstract function getName(); } diff --git a/src/AntCMS/AntPluginLoader.php b/src/AntCMS/AntPluginLoader.php index 84d9a4b..0e39c1f 100644 --- a/src/AntCMS/AntPluginLoader.php +++ b/src/AntCMS/AntPluginLoader.php @@ -6,6 +6,7 @@ use AntCMS\AntTools; class AntPluginLoader { + /** @return array */ public function loadPlugins() { $plugins = array(); diff --git a/src/AntCMS/AntTools.php b/src/AntCMS/AntTools.php index 0d047e3..057287b 100644 --- a/src/AntCMS/AntTools.php +++ b/src/AntCMS/AntTools.php @@ -4,7 +4,13 @@ namespace AntCMS; class AntTools { - public static function getFileList($dir, $extension = null, $returnPath = false) + /** + * @param string $dir + * @param (null|string)|null $extension + * @param null|bool $returnPath + * @return array + */ + public static function getFileList(string $dir, ?string $extension = null, ?bool $returnPath = false) { $dir = new \RecursiveDirectoryIterator($dir); $iterator = new \RecursiveIteratorIterator($dir); @@ -17,7 +23,11 @@ class AntTools return $files; } - public static function repairFilePath($path) + /** + * @param string $path + * @return string + */ + public static function repairFilePath(string $path) { $newPath = realpath($path); if (!$newPath) { @@ -30,20 +40,18 @@ class AntTools return $newPath; } + /** + * Repairs a URL by replacing backslashes with forward slashes and removing duplicate slashes. + * + * @param string $url The URL to repair. Note: this function will not work correctly if the URL provided has its own protocol (like HTTS://). + * @return string The repaired URL + */ + public static function repairURL(string $url) + { + $newURL = str_replace('\\\\', '/', $url); + $newURL = str_replace('\\', '/', $newURL); + $newURL = str_replace('//', '/', $newURL); -/** - * Repairs a URL by replacing backslashes with forward slashes and removing duplicate slashes. - * - * @param string $url The URL to repair. Note: this function will not work correctly if the URL provided has its own protocol (like HTTS://). - * @return string The repaired URL - */ -public static function repairURL($url) -{ - $newURL = str_replace('\\\\', '/', $url); - $newURL = str_replace('\\', '/', $newURL); - $newURL = str_replace('//', '/', $newURL); - - return $newURL; -} - + return $newURL; + } } diff --git a/src/AntCMS/AntTwig.php b/src/AntCMS/AntTwig.php index 092334a..4ef3cf5 100644 --- a/src/AntCMS/AntTwig.php +++ b/src/AntCMS/AntTwig.php @@ -6,6 +6,12 @@ use AntCMS\AntConfig; class AntTwig { + /** + * @param string $content + * @param array $params + * @param string|null $theme + * @return string + */ public function renderWithTiwg(string $content = '', array $params = array(), string $theme = null) { $twigCache = AntConfig::currentConfig('enableCache') ? AntCachePath : false; diff --git a/src/AntCMS/AntYaml.php b/src/AntCMS/AntYaml.php index 6a7d1c1..41e82d0 100644 --- a/src/AntCMS/AntYaml.php +++ b/src/AntCMS/AntYaml.php @@ -7,18 +7,31 @@ use Symfony\Component\Yaml\Yaml; class AntYaml { - public static function parseFile($file) + /** + * @param string $file + * @return array + */ + public static function parseFile(string $file) { return Yaml::parseFile($file); } - public static function saveFile($file, $data) + /** + * @param string $file + * @param array $data + * @return void + */ + public static function saveFile(string $file, array $data) { $yaml = Yaml::dump($data); file_put_contents($file, $yaml); } - public static function parseYaml($yaml){ + /** + * @param string $yaml + * @return array|null + */ + public static function parseYaml(string $yaml){ try { return Yaml::parse($yaml); } catch (ParseException $exception) { diff --git a/src/Plugins/Admin/AdminPlugin.php b/src/Plugins/Admin/AdminPlugin.php index c854e43..af59a74 100644 --- a/src/Plugins/Admin/AdminPlugin.php +++ b/src/Plugins/Admin/AdminPlugin.php @@ -11,6 +11,10 @@ use AntCMS\AntTwig; class AdminPlugin extends AntPlugin { + /** + * @param array $route + * @return void + */ public function handlePluginRoute(array $route) { AntAuth::checkAuth(); @@ -23,11 +27,9 @@ class AdminPlugin extends AntPlugin switch ($currentStep) { case 'config': $this->configureAntCMS($route); - break; case 'pages': $this->managePages($route); - break; default: $antTwig = new AntTwig; @@ -49,11 +51,16 @@ class AdminPlugin extends AntPlugin } } + /** @return string */ public function getName() { return 'Admin'; } + /** + * @param array $route + * @return never + */ private function configureAntCMS(array $route) { $antCMS = new AntCMS; @@ -113,6 +120,10 @@ class AdminPlugin extends AntPlugin exit; } + /** + * @param array $route + * @return never + */ private function managePages(array $route) { $antCMS = new AntCMS; @@ -199,7 +210,11 @@ class AdminPlugin extends AntPlugin exit; } - private function boolToWord($value) + /** + * @param bool $value + * @return string + */ + private function boolToWord(bool $value) { return boolval($value) ? 'true' : 'false'; } diff --git a/src/cron.php b/src/cron.php index a8a1cdc..9e7c6ae 100644 --- a/src/cron.php +++ b/src/cron.php @@ -1,5 +1,5 @@