From 7dd3dede145dd3af6296520a0bc936f99ca2916c Mon Sep 17 00:00:00 2001 From: Maksim Spirkov <63721828+mspirkov@users.noreply.github.com> Date: Mon, 3 Nov 2025 12:59:23 +0300 Subject: [PATCH] Add generics for all controllers (#20675) --- build/controllers/ClassmapController.php | 3 ++ build/controllers/DevController.php | 3 ++ build/controllers/MimeTypeController.php | 3 ++ build/controllers/PhpDocController.php | 3 ++ build/controllers/ReleaseController.php | 3 ++ build/controllers/TranslationController.php | 3 ++ build/controllers/Utf8Controller.php | 3 ++ framework/CHANGELOG.md | 1 + framework/base/ActionFilter.php | 24 ++++----- framework/base/Application.php | 7 ++- framework/base/Controller.php | 8 +++ framework/base/Module.php | 21 +++++--- framework/captcha/CaptchaValidator.php | 12 +++-- framework/console/Application.php | 4 ++ framework/console/Controller.php | 7 ++- framework/console/UnknownCommandException.php | 10 +++- .../console/controllers/AssetController.php | 4 ++ .../controllers/BaseMigrateController.php | 4 ++ .../console/controllers/CacheController.php | 4 ++ .../console/controllers/FixtureController.php | 4 ++ .../console/controllers/HelpController.php | 52 ++++++++++++++++--- .../console/controllers/MessageController.php | 7 ++- .../console/controllers/MigrateController.php | 4 ++ .../console/controllers/ServeController.php | 4 ++ framework/filters/AccessRule.php | 16 +++--- framework/filters/Cors.php | 5 +- framework/filters/HostControl.php | 5 +- framework/filters/RateLimitInterface.php | 13 ++--- framework/filters/RateLimiter.php | 5 +- framework/filters/VerbFilter.php | 5 +- framework/filters/auth/AuthMethod.php | 5 +- framework/rest/ActiveController.php | 4 ++ framework/rest/Controller.php | 7 ++- framework/web/Application.php | 4 ++ framework/web/Controller.php | 7 ++- phpstan-baseline.neon | 5 -- 36 files changed, 216 insertions(+), 63 deletions(-) diff --git a/build/controllers/ClassmapController.php b/build/controllers/ClassmapController.php index 978504a86f..4777a1de26 100644 --- a/build/controllers/ClassmapController.php +++ b/build/controllers/ClassmapController.php @@ -7,6 +7,7 @@ namespace yii\build\controllers; +use yii\console\Application; use yii\console\Controller; use yii\console\Exception; use yii\helpers\FileHelper; @@ -16,6 +17,8 @@ use yii\helpers\FileHelper; * * @author Qiang Xue * @since 2.0 + * + * @extends Controller */ class ClassmapController extends Controller { diff --git a/build/controllers/DevController.php b/build/controllers/DevController.php index 0e87f2275f..ea940dbba9 100644 --- a/build/controllers/DevController.php +++ b/build/controllers/DevController.php @@ -9,6 +9,7 @@ namespace yii\build\controllers; use Yii; use yii\base\InvalidParamException; +use yii\console\Application; use yii\console\Controller; use yii\console\ExitCode; use yii\helpers\Console; @@ -22,6 +23,8 @@ use yii\helpers\FileHelper; * * @author Carsten Brandt * @since 2.0 + * + * @extends Controller */ class DevController extends Controller { diff --git a/build/controllers/MimeTypeController.php b/build/controllers/MimeTypeController.php index a16f160819..e3534c9229 100644 --- a/build/controllers/MimeTypeController.php +++ b/build/controllers/MimeTypeController.php @@ -9,6 +9,7 @@ namespace yii\build\controllers; use Yii; +use yii\console\Application; use yii\console\Controller; use yii\helpers\Console; use yii\helpers\VarDumper; @@ -24,6 +25,8 @@ use yii\helpers\VarDumper; * * @author Carsten Brandt * @since 2.0 + * + * @extends Controller */ class MimeTypeController extends Controller { diff --git a/build/controllers/PhpDocController.php b/build/controllers/PhpDocController.php index f19fcd16f2..c38130b9c7 100644 --- a/build/controllers/PhpDocController.php +++ b/build/controllers/PhpDocController.php @@ -11,6 +11,7 @@ namespace yii\build\controllers; use Yii; use yii\base\Model; use yii\base\Module; +use yii\console\Application; use yii\console\Controller as ConsoleController; use yii\db\QueryBuilder; use yii\helpers\Console; @@ -27,6 +28,8 @@ use yii\web\Request as WebRequest; * @author Carsten Brandt * @author Alexander Makarov * @since 2.0 + * + * @extends ConsoleController */ class PhpDocController extends ConsoleController { diff --git a/build/controllers/ReleaseController.php b/build/controllers/ReleaseController.php index 79780c0179..56f078a7ac 100644 --- a/build/controllers/ReleaseController.php +++ b/build/controllers/ReleaseController.php @@ -10,6 +10,7 @@ namespace yii\build\controllers; use Yii; use yii\base\Exception; +use yii\console\Application; use yii\console\Controller; use yii\helpers\ArrayHelper; use yii\helpers\Console; @@ -40,6 +41,8 @@ use yii\helpers\FileHelper; * * @author Carsten Brandt * @since 2.0 + * + * @extends Controller */ class ReleaseController extends Controller { diff --git a/build/controllers/TranslationController.php b/build/controllers/TranslationController.php index 2d181fe42b..3bbedb1ebb 100644 --- a/build/controllers/TranslationController.php +++ b/build/controllers/TranslationController.php @@ -8,6 +8,7 @@ namespace yii\build\controllers; use DirectoryIterator; +use yii\console\Application; use yii\console\Controller; use yii\helpers\Html; @@ -17,6 +18,8 @@ use yii\helpers\Html; * build translation "../docs/guide" "../docs/guide-ru" "Russian guide translation report" > report_guide_ru.html * * @author Alexander Makarov + * + * @extends Controller */ class TranslationController extends Controller { diff --git a/build/controllers/Utf8Controller.php b/build/controllers/Utf8Controller.php index 7d159634a6..9545ed5865 100644 --- a/build/controllers/Utf8Controller.php +++ b/build/controllers/Utf8Controller.php @@ -8,6 +8,7 @@ namespace yii\build\controllers; +use yii\console\Application; use yii\console\Controller; use yii\helpers\Console; use yii\helpers\FileHelper; @@ -16,6 +17,8 @@ use yii\helpers\FileHelper; * Check files for broken UTF8 and non-printable characters. * * @author Carsten Brandt + * + * @extends Controller */ class Utf8Controller extends Controller { diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index fa7f7f9485..eb8b731e94 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -83,6 +83,7 @@ Yii Framework 2 Change Log - Bug #20666: Add missing generics in `yii\base`, `yii\console`, `yii\filters` and `yii\web` namespaces (mspirkov) - Bug #20673: Sanitize `null` bytes before `quoteValue()` on PHP 8.5+ in SQLite (terabytesoftw) - Bug #20671: Fix PHPDoc annotations in `yii\base`, `yii\console`, `yii\web` and `yii\widgets` namespaces (mspirkov) +- Bug #20675: Add generics for all controllers (mspirkov) 2.0.53 June 27, 2025 diff --git a/framework/base/ActionFilter.php b/framework/base/ActionFilter.php index 13b9ccae67..9ce524430a 100644 --- a/framework/base/ActionFilter.php +++ b/framework/base/ActionFilter.php @@ -71,8 +71,8 @@ class ActionFilter extends Behavior /** * @param ActionEvent $event * - * @phpstan-param ActionEvent> $event - * @psalm-param ActionEvent> $event + * @phpstan-param ActionEvent>> $event + * @psalm-param ActionEvent>> $event */ public function beforeFilter($event) { @@ -93,8 +93,8 @@ class ActionFilter extends Behavior /** * @param ActionEvent $event * - * @phpstan-param ActionEvent> $event - * @psalm-param ActionEvent> $event + * @phpstan-param ActionEvent>> $event + * @psalm-param ActionEvent>> $event */ public function afterFilter($event) { @@ -108,8 +108,8 @@ class ActionFilter extends Behavior * @param Action $action the action to be executed. * @return bool whether the action should continue to be executed. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function beforeAction($action) { @@ -123,8 +123,8 @@ class ActionFilter extends Behavior * @param mixed $result the action execution result * @return mixed the processed action result. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function afterAction($action, $result) { @@ -137,8 +137,8 @@ class ActionFilter extends Behavior * @return string * @since 2.0.7 * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function getActionId($action) { @@ -160,8 +160,8 @@ class ActionFilter extends Behavior * @param Action $action the action being filtered * @return bool whether the filter is active for the given action. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function isActive($action) { diff --git a/framework/base/Application.php b/framework/base/Application.php index 3f09515002..eae1488c50 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -114,6 +114,9 @@ abstract class Application extends Module public $sourceLanguage = 'en-US'; /** * @var Controller|null the currently active controller instance + * + * @phpstan-var Controller|null + * @psalm-var Controller|null */ public $controller; /** @@ -128,8 +131,8 @@ abstract class Application extends Module /** * @var Action|null the requested Action. If null, it means the request cannot be resolved into an action. * - * @phpstan-var Action|null - * @psalm-var Action|null + * @phpstan-var Action>|null + * @psalm-var Action>|null */ public $requestedAction; /** diff --git a/framework/base/Controller.php b/framework/base/Controller.php index dddb5d3b68..0bce3235c3 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -24,6 +24,8 @@ use yii\di\NotInstantiableException; * * @author Qiang Xue * @since 2.0 + * + * @template T of Module */ class Controller extends Component implements ViewContextInterface { @@ -43,6 +45,9 @@ class Controller extends Component implements ViewContextInterface public $id; /** * @var Module the module that this controller belongs to. + * + * @phpstan-var T + * @psalm-var T */ public $module; /** @@ -91,6 +96,9 @@ class Controller extends Component implements ViewContextInterface * @param Module $module the module that this controller belongs to. * @param array $config name-value pairs that will be used to initialize the object properties. * + * @phpstan-param T $module + * @psalm-param T $module + * * @phpstan-param array $config * @psalm-param array $config */ diff --git a/framework/base/Module.php b/framework/base/Module.php index e38d822343..030f43b943 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -545,7 +545,10 @@ class Module extends ServiceLocator { $parts = $this->createController($route); if (is_array($parts)) { - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller<$this> $controller + */ list($controller, $actionID) = $parts; $oldController = Yii::$app->controller; Yii::$app->controller = $controller; @@ -579,9 +582,12 @@ class Module extends ServiceLocator * part of the route which will be treated as the action ID. Otherwise, `false` will be returned. * * @param string $route the route consisting of module, controller and action IDs. - * @return array|bool If the controller is created successfully, it will be returned together + * @return array|false If the controller is created successfully, it will be returned together * with the requested action ID. Otherwise `false` will be returned. * @throws InvalidConfigException if the controller class and its file do not match. + * + * @phpstan-return array{Controller<$this>, string}|false + * @psalm-return array{Controller<$this>, string}|false */ public function createController($route) { @@ -638,6 +644,9 @@ class Module extends ServiceLocator * @return Controller|null the newly created controller instance, or `null` if the controller ID is invalid. * @throws InvalidConfigException if the controller class and its file name do not match. * This exception is only thrown when in debug mode. + * + * @phpstan-return Controller<$this>|null + * @psalm-return Controller<$this>|null */ public function createControllerByID($id) { @@ -718,8 +727,8 @@ class Module extends ServiceLocator * @param Action $action the action to be executed. * @return bool whether the action should continue to be executed. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function beforeAction($action) { @@ -749,8 +758,8 @@ class Module extends ServiceLocator * @param mixed $result the action return result. * @return mixed the processed action result. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function afterAction($action, $result) { diff --git a/framework/captcha/CaptchaValidator.php b/framework/captcha/CaptchaValidator.php index aa75089e04..b9b116de7e 100644 --- a/framework/captcha/CaptchaValidator.php +++ b/framework/captcha/CaptchaValidator.php @@ -9,6 +9,7 @@ namespace yii\captcha; use Yii; use yii\base\InvalidConfigException; +use yii\base\Module; use yii\helpers\Json; use yii\validators\ValidationAsset; use yii\validators\Validator; @@ -69,18 +70,21 @@ class CaptchaValidator extends Validator * @return CaptchaAction the action object * @throws InvalidConfigException * - * @phpstan-return CaptchaAction - * @psalm-return CaptchaAction + * @phpstan-return CaptchaAction> + * @psalm-return CaptchaAction> */ public function createCaptchaAction() { $ca = Yii::$app->createController($this->captchaAction); if ($ca !== false) { - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $ca; /** * @var CaptchaAction|null - * @phpstan-var CaptchaAction|null + * @phpstan-var CaptchaAction>|null */ $action = $controller->createAction($actionID); if ($action !== null) { diff --git a/framework/console/Application.php b/framework/console/Application.php index 59487d1683..8274503e41 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -9,6 +9,7 @@ namespace yii\console; use Yii; use yii\base\InvalidRouteException; +use yii\base\Module; // define STDIN, STDOUT and STDERR if the PHP SAPI did not define them (e.g. creating console application in web env) // https://www.php.net/manual/en/features.commandline.io-streams.php @@ -76,6 +77,9 @@ class Application extends \yii\base\Application public $enableCoreCommands = true; /** * @var Controller|null the currently active controller instance + * + * @phpstan-var Controller|null + * @psalm-var Controller|null */ public $controller; diff --git a/framework/console/Controller.php b/framework/console/Controller.php index f205e72e17..33e3dd3a82 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -13,6 +13,8 @@ use yii\base\InlineAction; use yii\base\InvalidRouteException; use yii\helpers\Console; use yii\helpers\Inflector; +use yii\base\Controller as BaseController; +use yii\base\Module; /** * Controller is the base class of console command classes. @@ -37,8 +39,11 @@ use yii\helpers\Inflector; * * @author Qiang Xue * @since 2.0 + * + * @template T of Module + * @extends BaseController */ -class Controller extends \yii\base\Controller +class Controller extends BaseController { /** * @deprecated since 2.0.13. Use [[ExitCode::OK]] instead. diff --git a/framework/console/UnknownCommandException.php b/framework/console/UnknownCommandException.php index a0e09674b4..3302346422 100644 --- a/framework/console/UnknownCommandException.php +++ b/framework/console/UnknownCommandException.php @@ -70,13 +70,19 @@ class UnknownCommandException extends Exception if ($help === false || $this->command === '') { return []; } - /** @var HelpController $helpController */ + /** + * @var HelpController $helpController + * @phpstan-var HelpController $helpController + */ list($helpController, $actionID) = $help; $availableActions = []; foreach ($helpController->getCommands() as $command) { $result = $this->application->createController($command); - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; if ($controller->createAction($controller->defaultAction) !== null) { // add the command itself (default action) diff --git a/framework/console/controllers/AssetController.php b/framework/console/controllers/AssetController.php index 7b451cb4e3..f73f0351df 100644 --- a/framework/console/controllers/AssetController.php +++ b/framework/console/controllers/AssetController.php @@ -8,6 +8,7 @@ namespace yii\console\controllers; use Yii; +use yii\console\Application; use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; @@ -44,6 +45,9 @@ use yii\web\AssetBundle; * @author Qiang Xue * @author Paul Klimov * @since 2.0 + * + * @template T of Application + * @extends Controller */ class AssetController extends Controller { diff --git a/framework/console/controllers/BaseMigrateController.php b/framework/console/controllers/BaseMigrateController.php index 7eebad42fe..2db2fc3254 100644 --- a/framework/console/controllers/BaseMigrateController.php +++ b/framework/console/controllers/BaseMigrateController.php @@ -12,6 +12,7 @@ use yii\base\Action; use yii\base\BaseObject; use yii\base\InvalidConfigException; use yii\base\NotSupportedException; +use yii\console\Application; use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; @@ -25,6 +26,9 @@ use yii\helpers\Inflector; * * @author Qiang Xue * @since 2.0 + * + * @template T of Application + * @extends Controller */ abstract class BaseMigrateController extends Controller { diff --git a/framework/console/controllers/CacheController.php b/framework/console/controllers/CacheController.php index 3dd374572e..4c862d58f0 100644 --- a/framework/console/controllers/CacheController.php +++ b/framework/console/controllers/CacheController.php @@ -10,6 +10,7 @@ namespace yii\console\controllers; use Yii; use yii\caching\ApcCache; use yii\caching\CacheInterface; +use yii\console\Application; use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; @@ -43,6 +44,9 @@ use yii\helpers\Console; * @author Alexander Makarov * @author Mark Jebri * @since 2.0 + * + * @template T of Application + * @extends Controller */ class CacheController extends Controller { diff --git a/framework/console/controllers/FixtureController.php b/framework/console/controllers/FixtureController.php index f8abdd01ed..a587b69ec8 100644 --- a/framework/console/controllers/FixtureController.php +++ b/framework/console/controllers/FixtureController.php @@ -10,6 +10,7 @@ namespace yii\console\controllers; use Yii; use yii\base\InvalidConfigException; use yii\base\InvalidParamException; +use yii\console\Application; use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; @@ -42,6 +43,9 @@ use yii\test\FixtureTrait; * * @author Mark Jebri * @since 2.0 + * + * @template T of Application + * @extends Controller */ class FixtureController extends Controller { diff --git a/framework/console/controllers/HelpController.php b/framework/console/controllers/HelpController.php index d243df02e3..4d4df0372b 100644 --- a/framework/console/controllers/HelpController.php +++ b/framework/console/controllers/HelpController.php @@ -9,11 +9,13 @@ namespace yii\console\controllers; use Yii; use yii\base\Application; +use yii\base\Module; use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; use yii\helpers\Console; use yii\helpers\Inflector; +use yii\console\Application as ConsoleApplication; /** * Provides help information about console commands. @@ -35,6 +37,9 @@ use yii\helpers\Inflector; * * @author Qiang Xue * @since 2.0 + * + * @template T of ConsoleApplication + * @extends Controller */ class HelpController extends Controller { @@ -80,7 +85,10 @@ class HelpController extends Controller { foreach ($this->getCommandDescriptions() as $command => $description) { $result = Yii::$app->createController($command); - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $actions = $this->getActions($controller); $prefix = $controller->getUniqueId(); @@ -108,7 +116,10 @@ class HelpController extends Controller return; } - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $action = $controller->createAction($actionID); if ($action === null) { @@ -141,7 +152,10 @@ class HelpController extends Controller return; } - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $action = $controller->createAction($actionID); if ($action === null) { @@ -179,6 +193,10 @@ class HelpController extends Controller if ($result === false || !$result[0] instanceof Controller) { return false; } + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $actions = $this->getActions($controller); return $actions !== []; @@ -194,7 +212,10 @@ class HelpController extends Controller $descriptions = []; foreach ($this->getCommands() as $command) { $result = Yii::$app->createController($command); - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $descriptions[$command] = $controller->getHelpSummary(); } @@ -206,6 +227,9 @@ class HelpController extends Controller * Returns all available actions of the specified controller. * @param Controller $controller the controller instance * @return array all available action IDs. + * + * @phpstan-param Controller $controller + * @psalm-param Controller $controller */ public function getActions($controller) { @@ -224,7 +248,7 @@ class HelpController extends Controller /** * Returns available commands of a specified module. - * @param \yii\base\Module $module the module instance + * @param Module $module the module instance * @return array the available command names */ protected function getModuleCommands($module) @@ -303,7 +327,10 @@ class HelpController extends Controller $maxLength = 0; foreach ($commands as $command => $description) { $result = Yii::$app->createController($command); - /** @var Controller $controller */ + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $actions = $this->getActions($controller); $prefix = $controller->getUniqueId(); @@ -317,6 +344,10 @@ class HelpController extends Controller } foreach ($commands as $command => $description) { $result = Yii::$app->createController($command); + /** + * @var Controller $controller + * @phpstan-var Controller $controller + */ list($controller, $actionID) = $result; $actions = $this->getActions($controller); $this->stdout('- ' . $this->ansiFormat($command, Console::FG_YELLOW)); @@ -349,6 +380,9 @@ class HelpController extends Controller /** * Displays the overall information of the command. * @param Controller $controller the controller instance + * + * @phpstan-param Controller $controller + * @psalm-param Controller $controller */ protected function getCommandHelp($controller) { @@ -395,6 +429,9 @@ class HelpController extends Controller * @param Controller $controller the controller instance * @param string $actionID action ID * @throws Exception if the action does not exist + * + * @phpstan-param Controller $controller + * @psalm-param Controller $controller */ protected function getSubCommandHelp($controller, $actionID) { @@ -517,6 +554,9 @@ class HelpController extends Controller * @param string $option the option name * @return string the formatted string for the alias argument or option * @since 2.0.8 + * + * @phpstan-param Controller $controller + * @psalm-param Controller $controller */ protected function formatOptionAliases($controller, $option) { diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index 63d3aa10bc..4feed0689f 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -8,6 +8,8 @@ namespace yii\console\controllers; use Yii; +use yii\console\Application; +use yii\console\Controller; use yii\console\Exception; use yii\console\ExitCode; use yii\db\Connection; @@ -37,8 +39,11 @@ use yii\i18n\GettextPoFile; * * @author Qiang Xue * @since 2.0 + * + * @template T of Application + * @extends Controller */ -class MessageController extends \yii\console\Controller +class MessageController extends Controller { /** * @var string controller default action ID. diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index e665d915b8..419f567971 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -9,6 +9,7 @@ namespace yii\console\controllers; use Yii; use yii\base\Action; +use yii\console\Application; use yii\db\Connection; use yii\db\Query; use yii\di\Instance; @@ -72,6 +73,9 @@ use yii\helpers\Inflector; * * @author Qiang Xue * @since 2.0 + * + * @template T of Application + * @extends BaseMigrateController */ class MigrateController extends BaseMigrateController { diff --git a/framework/console/controllers/ServeController.php b/framework/console/controllers/ServeController.php index 0e2ede8d8f..93218c7be8 100644 --- a/framework/console/controllers/ServeController.php +++ b/framework/console/controllers/ServeController.php @@ -8,6 +8,7 @@ namespace yii\console\controllers; use Yii; +use yii\console\Application; use yii\console\Controller; use yii\console\ExitCode; use yii\helpers\Console; @@ -20,6 +21,9 @@ use yii\helpers\Console; * * @author Alexander Makarov * @since 2.0.7 + * + * @template T of Application + * @extends Controller */ class ServeController extends Controller { diff --git a/framework/filters/AccessRule.php b/framework/filters/AccessRule.php index bd48300f2b..a0f3742695 100644 --- a/framework/filters/AccessRule.php +++ b/framework/filters/AccessRule.php @@ -12,6 +12,7 @@ use yii\base\Action; use yii\base\Component; use yii\base\Controller; use yii\base\InvalidConfigException; +use yii\base\Module; use yii\helpers\IpHelper; use yii\helpers\StringHelper; use yii\web\IdentityInterface; @@ -168,8 +169,8 @@ class AccessRule extends Component * @param Request $request * @return bool|null `true` if the user is allowed, `false` if the user is denied, `null` if the rule does not apply to the user * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action * * @phpstan-param User|false $user * @psalm-param User|false $user @@ -194,8 +195,8 @@ class AccessRule extends Component * @param Action $action the action * @return bool whether the rule applies to the action * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function matchAction($action) { @@ -205,6 +206,9 @@ class AccessRule extends Component /** * @param Controller $controller the controller * @return bool whether the rule applies to the controller + * + * @phpstan-param Controller $controller + * @psalm-param Controller $controller */ protected function matchController($controller) { @@ -311,8 +315,8 @@ class AccessRule extends Component * @param Action $action the action to be performed * @return bool whether the rule should be applied * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function matchCustom($action) { diff --git a/framework/filters/Cors.php b/framework/filters/Cors.php index 6ddf98b7f0..2be6592afb 100644 --- a/framework/filters/Cors.php +++ b/framework/filters/Cors.php @@ -13,6 +13,7 @@ use yii\base\ActionFilter; use yii\base\Component; use yii\base\Controller; use yii\base\InvalidConfigException; +use yii\base\Module; use yii\web\Request; use yii\web\Response; @@ -127,8 +128,8 @@ class Cors extends ActionFilter * Override settings for specific action. * @param Action $action the action settings to override * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function overrideDefaultSettings($action) { diff --git a/framework/filters/HostControl.php b/framework/filters/HostControl.php index 0f320b9f5d..871f682808 100644 --- a/framework/filters/HostControl.php +++ b/framework/filters/HostControl.php @@ -12,6 +12,7 @@ use yii\base\Action; use yii\base\ActionFilter; use yii\base\Component; use yii\base\Controller; +use yii\base\Module; use yii\helpers\StringHelper; use yii\web\NotFoundHttpException; @@ -169,8 +170,8 @@ class HostControl extends ActionFilter * @param Action $action the action to be executed. * @throws NotFoundHttpException * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function denyAccess($action) { diff --git a/framework/filters/RateLimitInterface.php b/framework/filters/RateLimitInterface.php index 0985967f5e..285a64439a 100644 --- a/framework/filters/RateLimitInterface.php +++ b/framework/filters/RateLimitInterface.php @@ -9,6 +9,7 @@ namespace yii\filters; use yii\base\Action; use yii\base\Controller; +use yii\base\Module; /** * RateLimitInterface is the interface that may be implemented by an identity object to enforce rate limiting. @@ -25,8 +26,8 @@ interface RateLimitInterface * @return array an array of two elements. The first element is the maximum number of allowed requests, * and the second element is the size of the window in seconds. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function getRateLimit($request, $action); @@ -37,8 +38,8 @@ interface RateLimitInterface * @return array an array of two elements. The first element is the number of allowed requests, * and the second element is the corresponding UNIX timestamp. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function loadAllowance($request, $action); @@ -49,8 +50,8 @@ interface RateLimitInterface * @param int $allowance the number of allowed requests remaining. * @param int $timestamp the current timestamp. * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function saveAllowance($request, $action, $allowance, $timestamp); } diff --git a/framework/filters/RateLimiter.php b/framework/filters/RateLimiter.php index 89774f0a3f..1be699bc10 100644 --- a/framework/filters/RateLimiter.php +++ b/framework/filters/RateLimiter.php @@ -13,6 +13,7 @@ use yii\base\Action; use yii\base\ActionFilter; use yii\base\Component; use yii\base\Controller; +use yii\base\Module; use yii\web\IdentityInterface; use yii\web\Request; use yii\web\Response; @@ -122,8 +123,8 @@ class RateLimiter extends ActionFilter * @param Action $action the action to be executed * @throws TooManyRequestsHttpException if rate limit exceeds * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ public function checkRateLimit($user, $request, $response, $action) { diff --git a/framework/filters/VerbFilter.php b/framework/filters/VerbFilter.php index f97f3ec83d..767f596a79 100644 --- a/framework/filters/VerbFilter.php +++ b/framework/filters/VerbFilter.php @@ -11,6 +11,7 @@ use Yii; use yii\base\Action; use yii\base\ActionEvent; use yii\base\Behavior; +use yii\base\Module; use yii\web\Controller; use yii\web\MethodNotAllowedHttpException; @@ -91,8 +92,8 @@ class VerbFilter extends Behavior * @return bool * @throws MethodNotAllowedHttpException when the request method is not allowed. * - * @phpstan-param ActionEvent> $event - * @psalm-param ActionEvent> $event + * @phpstan-param ActionEvent>> $event + * @psalm-param ActionEvent>> $event */ public function beforeAction($event) { diff --git a/framework/filters/auth/AuthMethod.php b/framework/filters/auth/AuthMethod.php index e42756d6fc..2dee53c9e5 100644 --- a/framework/filters/auth/AuthMethod.php +++ b/framework/filters/auth/AuthMethod.php @@ -12,6 +12,7 @@ use yii\base\Action; use yii\base\ActionFilter; use yii\base\Component; use yii\base\Controller; +use yii\base\Module; use yii\helpers\StringHelper; use yii\web\IdentityInterface; use yii\web\Request; @@ -110,8 +111,8 @@ abstract class AuthMethod extends ActionFilter implements AuthInterface * @see optional * @since 2.0.7 * - * @phpstan-param Action $action - * @psalm-param Action $action + * @phpstan-param Action> $action + * @psalm-param Action> $action */ protected function isOptional($action) { diff --git a/framework/rest/ActiveController.php b/framework/rest/ActiveController.php index ae14947ac2..b29a477173 100644 --- a/framework/rest/ActiveController.php +++ b/framework/rest/ActiveController.php @@ -9,6 +9,7 @@ namespace yii\rest; use yii\base\InvalidConfigException; use yii\base\Model; +use yii\base\Module; use yii\web\ForbiddenHttpException; /** @@ -36,6 +37,9 @@ use yii\web\ForbiddenHttpException; * * @author Qiang Xue * @since 2.0 + * + * @template T of Module + * @extends Controller */ class ActiveController extends Controller { diff --git a/framework/rest/Controller.php b/framework/rest/Controller.php index 9512ee75e1..c919a9e096 100644 --- a/framework/rest/Controller.php +++ b/framework/rest/Controller.php @@ -13,6 +13,8 @@ use yii\filters\ContentNegotiator; use yii\filters\RateLimiter; use yii\filters\VerbFilter; use yii\web\Response; +use yii\web\Controller as WebController; +use yii\base\Module; /** * Controller is the base class for RESTful API controller classes. @@ -29,8 +31,11 @@ use yii\web\Response; * * @author Qiang Xue * @since 2.0 + * + * @template T of Module + * @extends WebController */ -class Controller extends \yii\web\Controller +class Controller extends WebController { /** * @var string|array the configuration for creating the serializer that formats the response data. diff --git a/framework/web/Application.php b/framework/web/Application.php index 2571f34ead..e0aabf96a1 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -9,6 +9,7 @@ namespace yii\web; use Yii; use yii\base\InvalidRouteException; +use yii\base\Module; use yii\helpers\Url; /** @@ -58,6 +59,9 @@ class Application extends \yii\base\Application public $catchAll; /** * @var Controller|null the currently active controller instance + * + * @phpstan-var Controller|null + * @psalm-var Controller|null */ public $controller; diff --git a/framework/web/Controller.php b/framework/web/Controller.php index 6b2983ef45..299e9fed57 100644 --- a/framework/web/Controller.php +++ b/framework/web/Controller.php @@ -12,6 +12,8 @@ use yii\base\Exception; use yii\base\InlineAction; use yii\helpers\Url; use yii\base\Action; +use yii\base\Controller as BaseController; +use yii\base\Module; /** * Controller is the base class of web controllers. @@ -24,8 +26,11 @@ use yii\base\Action; * * @author Qiang Xue * @since 2.0 + * + * @template T of Module + * @extends BaseController */ -class Controller extends \yii\base\Controller +class Controller extends BaseController { /** * @var bool whether to enable CSRF validation for the actions in this controller. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c79911603b..950aceea90 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -404,8 +404,3 @@ parameters: message: "#^Argument of an invalid type \\$this\\(yii\\\\caching\\\\Dependency\\) supplied for foreach\\, only iterables are supported\\.$#" count: 1 path: framework/caching/Dependency.php - - - - message: "#^Property yii\\\\console\\\\Application\\:\\:\\$controller \\(yii\\\\console\\\\Controller\\|yii\\\\web\\\\Controller\\|null\\) does not accept yii\\\\base\\\\Controller\\.$#" - count: 1 - path: framework/base/Module.php