From 6bc73a32c1fe185e0734182053008e6e19ddf1cf Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 20 Mar 2019 12:52:16 +0200 Subject: [PATCH] Fixed some issues found by phpstan, remove deprecated method calls (User) --- admin.php | 71 +++++----- classes/Twig/AdminTwigExtension.php | 1 + classes/admin.php | 127 +++++++++--------- classes/adminbasecontroller.php | 1 + classes/admincontroller.php | 68 ++++++---- classes/gpm.php | 13 +- classes/popularity.php | 1 + classes/themes.php | 1 + classes/utils.php | 28 ++-- composer.json | 6 + .../partials/nav-user-avatar.html.twig | 2 +- .../partials/userinfo-avatar.html.twig | 2 +- vendor/composer/ClassLoader.php | 4 +- vendor/composer/autoload_classmap.php | 9 ++ vendor/composer/autoload_static.php | 13 ++ 15 files changed, 182 insertions(+), 165 deletions(-) diff --git a/admin.php b/admin.php index f5b4bb4b..79581e0c 100644 --- a/admin.php +++ b/admin.php @@ -1,6 +1,8 @@ 1000, ]; - /** - * @var bool - */ + /** @var bool */ protected $active = false; - /** - * @var string - */ + /** @var string */ protected $template; - /** - * @var string - */ + /** @var string */ protected $theme; - /** - * @var string - */ + /** @var string */ protected $route; - /** - * @var string - */ + /** @var string */ protected $admin_route; - /** - * @var Uri - */ + /** @var Uri */ protected $uri; - /** - * @var Admin - */ + /** @var Admin */ protected $admin; - /** - * @var Session - */ + /** @var Session */ protected $session; - /** - * @var Popularity - */ + /** @var Popularity */ protected $popularity; - /** - * @var string - */ + /** @var string */ protected $base; + /** @var string */ protected $version; /** @@ -90,6 +73,7 @@ class AdminPlugin extends Plugin { return [ 'onPluginsInitialized' => [ + ['autoload', 100001], ['setup', 100000], ['onPluginsInitialized', 1001] ], @@ -130,6 +114,16 @@ class AdminPlugin extends Plugin ]; } + /** + * [onPluginsInitialized:100000] Composer autoload. + * + * @return ClassLoader + */ + public function autoload() + { + return require __DIR__ . '/vendor/autoload.php'; + } + /** * [onPluginsInitialized:100000] * @@ -138,13 +132,6 @@ class AdminPlugin extends Plugin */ public function setup() { - // Autoloader - spl_autoload_register(function ($class) { - if (Utils::startsWith($class, 'Grav\Plugin\Admin')) { - require_once __DIR__ .'/classes/' . strtolower(basename(str_replace("\\", '/', $class))) . '.php'; - } - }); - $route = $this->config->get('plugins.admin.route'); if (!$route) { return; @@ -197,10 +184,11 @@ class AdminPlugin extends Plugin // Only activate admin if we're inside the admin path. if ($this->active) { // Store this version. - $this->version = $this->getBlueprint()->version; + $this->version = $this->getBlueprint()->get('version'); // Have a unique Admin-only Cache key if (method_exists($this->grav['cache'], 'setKey')) { + /** @var Cache $cache */ $cache = $this->grav['cache']; $cache_key = $cache->getKey(); $cache->setKey($cache_key . '$'); @@ -311,8 +299,11 @@ class AdminPlugin extends Plugin $data['state'] = 'enabled'; $data['access'] = ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]]; + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + // Create user object and save it - $user = User::load($username); + $user = $users->load($username); $user->update($data); $user->save(); diff --git a/classes/Twig/AdminTwigExtension.php b/classes/Twig/AdminTwigExtension.php index c11328de..073aef23 100644 --- a/classes/Twig/AdminTwigExtension.php +++ b/classes/Twig/AdminTwigExtension.php @@ -1,4 +1,5 @@ enabled()) { $this->multilang = true; - $this->languages_enabled = $this->grav['config']->get('system.languages.supported', []); + $this->languages_enabled = (array)$this->grav['config']->get('system.languages.supported', []); //Set the currently active language for the admin $language = $this->grav['uri']->param('lang'); @@ -664,12 +651,18 @@ class Admin $data[$type] = $obj; } elseif (preg_match('|users/|', $type)) { - $obj = User::load(preg_replace('|users/|', '', $type)); + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + + $obj = $users->load(preg_replace('|users/|', '', $type)); $obj->update($this->cleanUserPost($post)); $data[$type] = $obj; } elseif (preg_match('|user/|', $type)) { - $obj = User::load(preg_replace('|user/|', '', $type)); + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + + $obj = $users->load(preg_replace('|user/|', '', $type)); $obj->update($this->cleanUserPost($post)); $data[$type] = $obj; @@ -691,7 +684,7 @@ class Admin $file = File::instance($filename); - $obj = new \StdClass(); + $obj = new \stdClass(); $obj->title = $file->basename(); $obj->path = $file->filename(); $obj->file = $file; @@ -718,7 +711,7 @@ class Admin /** * Clean user form post and remove extra stuff that may be passed along * - * @param $post + * @param array $post * @return array */ protected function cleanUserPost($post) @@ -800,7 +793,7 @@ class Admin /** * Count the pages * - * @return array + * @return int */ public function pagesCount() { @@ -996,7 +989,7 @@ class Admin /** * Check the passed packages list can be updated * - * @param $packages + * @param array $packages * * @throws \Exception * @return bool @@ -1168,7 +1161,7 @@ class Admin /** * Guest date format based on euro/US * - * @param $date + * @param string $date * * @return string */ @@ -1317,7 +1310,7 @@ class Admin /** * Sets the entire permissions array * - * @param $permissions + * @param array $permissions */ public function setPermissions($permissions) { @@ -1327,7 +1320,7 @@ class Admin /** * Adds a permission to the permissions array * - * @param $permissions + * @param array $permissions */ public function addPermissions($permissions) { @@ -1584,9 +1577,9 @@ class Admin /** * Returns the page creating it if it does not exist. * - * @param $path + * @param string $path * - * @return PageInterface + * @return PageInterface|null */ public function getPage($path) { diff --git a/classes/adminbasecontroller.php b/classes/adminbasecontroller.php index 49c45d34..542f7ee2 100644 --- a/classes/adminbasecontroller.php +++ b/classes/adminbasecontroller.php @@ -1,4 +1,5 @@ file(); if ($file->exists()) { - $content = $file->content(); + $content = (array)$file->content(); $content['twofa_secret'] = $secret; $file->save($content); $file->free(); } // Change secret in the session. - $user->twofa_secret = $secret; + $user->set('twofa_secret', $secret); $this->admin->json_response = ['status' => 'success', 'image' => $image, 'secret' => preg_replace('|(\w{4})|', '\\1 ', $secret)]; } catch (\Exception $e) { @@ -144,13 +146,16 @@ class AdminController extends AdminBaseController $data = $this->data; if (isset($data['password'])) { + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + $username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : null; - $user = $username ? User::load($username) : null; + $user = $username ? $users->load($username) : null; $password = $data['password'] ?? null; $token = $data['token'] ?? null; - if ($user && $user->exists() && !empty($user->reset)) { - list($good_token, $expire) = explode('::', $user->reset); + if ($user && $user->exists() && !empty($user->get('reset'))) { + list($good_token, $expire) = explode('::', $user->get('reset')); if ($good_token === $token) { if (time() > $expire) { @@ -160,8 +165,9 @@ class AdminController extends AdminBaseController return true; } - unset($user->hashed_password, $user->reset); - $user->password = $password; + $user->undef('hashed_password'); + $user->undef('reset'); + $user->set('password', $password); $user->validate(); $user->filter(); @@ -210,8 +216,11 @@ class AdminController extends AdminBaseController $data = $this->data; $login = $this->grav['login']; + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + $username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : ''; - $user = !empty($username) ? User::load($username) : null; + $user = !empty($username) ? $users->load($username) : null; if (!isset($this->grav['Email'])) { $this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error'); @@ -249,7 +258,7 @@ class AdminController extends AdminBaseController $token = md5(uniqid(mt_rand(), true)); $expire = time() + 604800; // next week - $user->reset = $token . '::' . $expire; + $user->set('reset', $token . '::' . $expire); $user->save(); $author = $this->grav['config']->get('site.author.name', ''); @@ -504,7 +513,7 @@ class AdminController extends AdminBaseController /** * Get the next available ordering number in a folder * - * @param $path + * @param string $path * * @return string the correct order string to prepend */ @@ -675,9 +684,12 @@ class AdminController extends AdminBaseController if ($this->view === 'user') { if ($obj->username === $this->grav['user']->username) { + /** @var UserCollectionInterface $users */ + $users = $this->grav['users']; + //Editing current user. Reload user object unset($this->grav['user']->avatar); - $this->grav['user']->merge(User::load($this->admin->route)->toArray()); + $this->grav['user']->merge($users->load($this->admin->route)->toArray()); } } } @@ -830,19 +842,16 @@ class AdminController extends AdminBaseController } // do we need to force a reload - $refresh = $this->data['refresh'] === 'true' ? true : false; + $refresh = $this->data['refresh'] === 'true'; $filter = $this->data['filter'] ?? ''; - - if (!empty($filter)) { - $filter_types = array_map('trim', explode(',', $filter)); - } + $filter_types = !empty($filter) ? array_map('trim', explode(',', $filter)) : []; try { $notifications = $this->admin->getNotifications($refresh); $notification_data = []; foreach ($notifications as $type => $type_notifications) { - if (empty($filter) || in_array($type, $filter_types)) { + if ($filter_types && in_array($type, $filter_types, true)) { $twig_template = 'partials/notification-' . $type . '-block.html.twig'; $notification_data[$type] = $this->grav['twig']->processTemplate($twig_template, ['notifications' => $type_notifications]); } @@ -1347,7 +1356,7 @@ class AdminController extends AdminBaseController if ($page) { $child_type = $page->childType(); - if (isset($child_type)) { + if ($child_type !== '') { $this->admin->json_response = [ 'status' => 'success', 'child_type' => $child_type @@ -1484,7 +1493,7 @@ class AdminController extends AdminBaseController 'message' => $this->admin::translate('PLUGIN_ADMIN.PAGES_FILTERED'), 'results' => $results ]; - $this->admin->collection = $collection; + $this->admin->collection = $collection; } /** @@ -1511,7 +1520,7 @@ class AdminController extends AdminBaseController $media_list = []; /** * @var string $name - * @var Medium $medium + * @var Medium|ImageMedium $medium */ foreach ($media->all() as $name => $medium) { @@ -1522,13 +1531,14 @@ class AdminController extends AdminBaseController } // Get original name - $source = $medium->higherQualityAlternative(); + /** @var ImageMedium $source */ + $source = method_exists($medium, 'higherQualityAlternative') ? $medium->higherQualityAlternative() : null; $media_list[$name] = [ 'url' => $medium->display($medium->get('extension') === 'svg' ? 'source' : 'thumbnail')->cropZoom(400, 300)->url(), 'size' => $medium->get('size'), 'metadata' => $metadata, - 'original' => $source->get('filename') + 'original' => $source ? $source->get('filename') : null ]; } @@ -1577,7 +1587,7 @@ class AdminController extends AdminBaseController /** @var Config $config */ $config = $this->grav['config']; - if (!isset($_FILES) || empty($_FILES)) { + if (empty($_FILES)) { $this->admin->json_response = [ 'status' => 'error', 'message' => $this->admin::translate('PLUGIN_ADMIN.EXCEEDED_POSTMAX_LIMIT') @@ -1681,7 +1691,7 @@ class AdminController extends AdminBaseController /** @var UniformResourceLocator $locator */ $locator = $this->grav['locator']; - $path = $media->path(); + $path = $media->getPath(); if ($locator->isStream($path)) { $path = $locator->findResource($path, true, true); } @@ -1763,7 +1773,7 @@ class AdminController extends AdminBaseController /** @var UniformResourceLocator $locator */ $locator = $this->grav['locator']; - $targetPath = $media->path() . '/' . $filename; + $targetPath = $media->getPath() . '/' . $filename; if ($locator->isStream($targetPath)) { $targetPath = $locator->findResource($targetPath, true, true); } @@ -1786,10 +1796,10 @@ class AdminController extends AdminBaseController } // Remove Extra Files - foreach (scandir($media->path(), SCANDIR_SORT_NONE) as $file) { + foreach (scandir($media->getPath(), SCANDIR_SORT_NONE) as $file) { if (preg_match("/{$fileParts['filename']}@\d+x\.{$fileParts['extension']}(?:\.meta\.yaml)?$|{$filename}\.meta\.yaml$/", $file)) { - $targetPath = $media->path() . '/' . $file; + $targetPath = $media->getPath() . '/' . $file; if ($locator->isStream($targetPath)) { $targetPath = $locator->findResource($targetPath, true, true); } @@ -1940,7 +1950,7 @@ class AdminController extends AdminBaseController }); } $page->header((object)$header); - $page->frontmatter(Yaml::dump((array)$page->header()), 20); + $page->frontmatter(Yaml::dump((array)$page->header(), 20)); } // Fill content last because it also renders the output. if (isset($input['content'])) { diff --git a/classes/gpm.php b/classes/gpm.php index 6959b8e7..dbaafba4 100644 --- a/classes/gpm.php +++ b/classes/gpm.php @@ -27,9 +27,6 @@ class Gpm { if (!static::$GPM) { static::$GPM = new GravGPM(); - if (method_exists('GravGPM', 'loadRemoteGrav')) { - static::$GPM->loadRemoteGrav(); - } } return static::$GPM; @@ -53,7 +50,7 @@ class Gpm * @param Package[]|string[]|string $packages * @param array $options * - * @return bool + * @return string|bool */ public static function install($packages, array $options) { @@ -128,7 +125,7 @@ class Gpm * @param Package[]|string[]|string $packages * @param array $options * - * @return bool + * @return string|bool */ public static function update($packages, array $options) { @@ -141,7 +138,7 @@ class Gpm * @param Package[]|string[]|string $packages * @param array $options * - * @return bool + * @return string|bool */ public static function uninstall($packages, array $options) { @@ -199,9 +196,9 @@ class Gpm /** * Direct install a file * - * @param $package_file + * @param string $package_file * - * @return bool + * @return string|bool */ public static function directInstall($package_file) { diff --git a/classes/popularity.php b/classes/popularity.php index d45672b2..7eef0cb1 100644 --- a/classes/popularity.php +++ b/classes/popularity.php @@ -1,4 +1,5 @@ findResource('account://'); - $files = array_diff(scandir($account_dir, SCANDIR_SORT_ASCENDING), ['.', '..']); + $grav = Grav::instance(); - foreach ($files as $file) { - if (strpos($file, '.yaml') !== false) { - $user = User::load(trim(substr($file, 0, -5))); - if ($user['email'] === $email) { - return $user; - } - } - } + /** @var UserCollectionInterface $users */ + $users = $grav['users']; - // If a User with the provided email cannot be found, then load user with that email as the username - return User::load($email); + return $users->find($email, ['email']); } /** @@ -42,7 +36,7 @@ class Utils * @param string $str * @return string */ - public static function slug($str) + public static function slug(string $str) { if (function_exists('transliterator_transliterate')) { $str = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;', $str); diff --git a/composer.json b/composer.json index e6f01141..d2fe5978 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,12 @@ "symfony/finder": "~4.1", "symfony/event-dispatcher": "~4.1" }, + "autoload": { + "classmap": [ + "classes/", + "admin.php" + ] + }, "config": { "platform": { "php": "7.1.3" diff --git a/themes/grav/templates/partials/nav-user-avatar.html.twig b/themes/grav/templates/partials/nav-user-avatar.html.twig index 03226eed..628d87ba 100644 --- a/themes/grav/templates/partials/nav-user-avatar.html.twig +++ b/themes/grav/templates/partials/nav-user-avatar.html.twig @@ -1 +1 @@ - + diff --git a/themes/grav/templates/partials/userinfo-avatar.html.twig b/themes/grav/templates/partials/userinfo-avatar.html.twig index 693cea6d..47240dd9 100644 --- a/themes/grav/templates/partials/userinfo-avatar.html.twig +++ b/themes/grav/templates/partials/userinfo-avatar.html.twig @@ -1,5 +1,5 @@ {% if data.avatar %} - + {% else %} {% endif %} \ No newline at end of file diff --git a/vendor/composer/ClassLoader.php b/vendor/composer/ClassLoader.php index fce8549f..dc02dfb1 100644 --- a/vendor/composer/ClassLoader.php +++ b/vendor/composer/ClassLoader.php @@ -279,7 +279,7 @@ class ClassLoader */ public function setApcuPrefix($apcuPrefix) { - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; + $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null; } /** @@ -377,7 +377,7 @@ class ClassLoader $subPath = $class; while (false !== $lastPos = strrpos($subPath, '\\')) { $subPath = substr($subPath, 0, $lastPos); - $search = $subPath . '\\'; + $search = $subPath.'\\'; if (isset($this->prefixDirsPsr4[$search])) { $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); foreach ($this->prefixDirsPsr4[$search] as $dir) { diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 7a91153b..7c79f9dc 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -6,4 +6,13 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'Grav\\Plugin\\AdminPlugin' => $baseDir . '/admin.php', + 'Grav\\Plugin\\Admin\\Admin' => $baseDir . '/classes/admin.php', + 'Grav\\Plugin\\Admin\\AdminBaseController' => $baseDir . '/classes/adminbasecontroller.php', + 'Grav\\Plugin\\Admin\\AdminController' => $baseDir . '/classes/admincontroller.php', + 'Grav\\Plugin\\Admin\\Gpm' => $baseDir . '/classes/gpm.php', + 'Grav\\Plugin\\Admin\\Popularity' => $baseDir . '/classes/popularity.php', + 'Grav\\Plugin\\Admin\\Themes' => $baseDir . '/classes/themes.php', + 'Grav\\Plugin\\Admin\\Twig\\AdminTwigExtension' => $baseDir . '/classes/Twig/AdminTwigExtension.php', + 'Grav\\Plugin\\Admin\\Utils' => $baseDir . '/classes/utils.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index efcbc3a2..ff8dd752 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -38,12 +38,25 @@ class ComposerStaticInitda370287ab6d5b8a28188afe08f659c5 ), ); + public static $classMap = array ( + 'Grav\\Plugin\\AdminPlugin' => __DIR__ . '/../..' . '/admin.php', + 'Grav\\Plugin\\Admin\\Admin' => __DIR__ . '/../..' . '/classes/admin.php', + 'Grav\\Plugin\\Admin\\AdminBaseController' => __DIR__ . '/../..' . '/classes/adminbasecontroller.php', + 'Grav\\Plugin\\Admin\\AdminController' => __DIR__ . '/../..' . '/classes/admincontroller.php', + 'Grav\\Plugin\\Admin\\Gpm' => __DIR__ . '/../..' . '/classes/gpm.php', + 'Grav\\Plugin\\Admin\\Popularity' => __DIR__ . '/../..' . '/classes/popularity.php', + 'Grav\\Plugin\\Admin\\Themes' => __DIR__ . '/../..' . '/classes/themes.php', + 'Grav\\Plugin\\Admin\\Twig\\AdminTwigExtension' => __DIR__ . '/../..' . '/classes/Twig/AdminTwigExtension.php', + 'Grav\\Plugin\\Admin\\Utils' => __DIR__ . '/../..' . '/classes/utils.php', + ); + public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { $loader->prefixLengthsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixLengthsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixDirsPsr4; $loader->prefixesPsr0 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixesPsr0; + $loader->classMap = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$classMap; }, null, ClassLoader::class); }