Add generics for all controllers (#20675)

This commit is contained in:
Maksim Spirkov
2025-11-03 12:59:23 +03:00
committed by GitHub
parent 0998ac6cb1
commit 7dd3dede14
36 changed files with 216 additions and 63 deletions

View File

@@ -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 <qiang.xue@gmail.com>
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*
* @template T of Application
* @extends Controller<T>
*/
class AssetController extends Controller
{

View File

@@ -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 <qiang.xue@gmail.com>
* @since 2.0
*
* @template T of Application
* @extends Controller<T>
*/
abstract class BaseMigrateController extends Controller
{

View File

@@ -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 <sam@rmcreative.ru>
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0
*
* @template T of Application
* @extends Controller<T>
*/
class CacheController extends Controller
{

View File

@@ -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 <mark.github@yandex.ru>
* @since 2.0
*
* @template T of Application
* @extends Controller<T>
*/
class FixtureController extends Controller
{

View File

@@ -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 <qiang.xue@gmail.com>
* @since 2.0
*
* @template T of ConsoleApplication
* @extends Controller<T>
*/
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<Application> $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<Application> $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<Application> $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<Application> $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<Application> $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<Module> $controller
* @psalm-param Controller<Module> $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<Application> $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<Application> $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<Module> $controller
* @psalm-param Controller<Module> $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<Module> $controller
* @psalm-param Controller<Module> $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<Module> $controller
* @psalm-param Controller<Module> $controller
*/
protected function formatOptionAliases($controller, $option)
{

View File

@@ -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 <qiang.xue@gmail.com>
* @since 2.0
*
* @template T of Application
* @extends Controller<T>
*/
class MessageController extends \yii\console\Controller
class MessageController extends Controller
{
/**
* @var string controller default action ID.

View File

@@ -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 <qiang.xue@gmail.com>
* @since 2.0
*
* @template T of Application
* @extends BaseMigrateController<T>
*/
class MigrateController extends BaseMigrateController
{

View File

@@ -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 <sam@rmcreative.ru>
* @since 2.0.7
*
* @template T of Application
* @extends Controller<T>
*/
class ServeController extends Controller
{