Skip to content

[Console][Mailer][Security] Added PHP type declarations #14591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ want a command to create a user::
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:create-user';

protected function configure()
protected function configure(): void
{
// ...
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// ... put here the code to create the user

Expand All @@ -65,7 +65,7 @@ You can optionally define a description, help message and the
:doc:`input options and arguments </console/input>`::

// ...
protected function configure()
protected function configure(): void
{
$this
// the short description shown while running "php bin/console list"
Expand Down Expand Up @@ -100,7 +100,7 @@ available in the ``configure()`` method::
parent::__construct();
}

protected function configure()
protected function configure(): void
{
$this
// ...
Expand Down Expand Up @@ -136,7 +136,7 @@ The ``execute()`` method has access to the output stream to write messages to
the console::

// ...
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// outputs multiple lines to the console (adding "\n" at the end of each line)
$output->writeln([
Expand Down Expand Up @@ -189,7 +189,7 @@ method, which returns an instance of

class MyCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$output instanceof ConsoleOutputInterface) {
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
Expand Down Expand Up @@ -236,7 +236,7 @@ Use input options or arguments to pass information to the command::
use Symfony\Component\Console\Input\InputArgument;

// ...
protected function configure()
protected function configure(): void
{
$this
// configure an argument
Expand All @@ -246,7 +246,7 @@ Use input options or arguments to pass information to the command::
}

// ...
public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln([
'User Creator',
Expand Down Expand Up @@ -300,7 +300,7 @@ as a service, you can use normal dependency injection. Imagine you have a

// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// ...

Expand Down
44 changes: 24 additions & 20 deletions console/calling_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,40 @@ or if you want to create a "meta" command that runs a bunch of other commands
changed on the production servers: clearing the cache, generating Doctrine
proxies, dumping web assets, ...).

Calling a command from another one is straightforward::
Use the :method:`Symfony\\Component\\Console\\Application::find` method to
find the command you want to run by passing the command name. Then, create a
new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the
arguments and options you want to pass to the command.

Eventually, calling the ``run()`` method actually runs the command and returns
the returned code from the command (return value from command's ``execute()``
method)::

// ...
use Symfony\Component\Console\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
// ...

protected function execute(InputInterface $input, OutputInterface $output)
class CreateUserCommand extends Command
{
$command = $this->getApplication()->find('demo:greet');

$arguments = [
'name' => 'Fabien',
'--yell' => true,
];
// ...

$greetInput = new ArrayInput($arguments);
$returnCode = $command->run($greetInput, $output);
protected function execute(InputInterface $input, OutputInterface $output): void
{
$command = $this->getApplication()->find('demo:greet');

// ...
}
$arguments = [
'name' => 'Fabien',
'--yell' => true,
];

First, you :method:`Symfony\\Component\\Console\\Application::find` the
command you want to run by passing the command name. Then, you need to create
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments
and options you want to pass to the command.
$greetInput = new ArrayInput($arguments);
$returnCode = $command->run($greetInput, $output);

Eventually, calling the ``run()`` method actually runs the command and returns
the returned code from the command (return value from command's ``execute()``
method).
// ...
}
}

.. tip::

Expand Down
4 changes: 2 additions & 2 deletions console/command_in_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Run this command from inside your controller via::

class SpoolController extends AbstractController
{
public function sendSpool($messages = 10, KernelInterface $kernel)
public function sendSpool(int $messages = 10, KernelInterface $kernel): Response
{
$application = new Application($kernel);
$application->setAutoExit(false);
Expand Down Expand Up @@ -87,7 +87,7 @@ Now, use it in your controller::

class SpoolController extends AbstractController
{
public function sendSpool($messages = 10)
public function sendSpool(int $messages = 10): Response
{
// ...
$output = new BufferedOutput(
Expand Down
6 changes: 3 additions & 3 deletions console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ For example, suppose you want to log something from within your command::
parent::__construct();
}

protected function configure()
protected function configure(): void
{
$this
->setDescription('Good morning!');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->info('Waking up the sun');
// ...

return 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion console/hide_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ In those cases, you can define the command as **hidden** by setting the
{
protected static $defaultName = 'app:legacy';

protected function configure()
protected function configure(): void
{
$this
->setHidden(true)
Expand Down
6 changes: 4 additions & 2 deletions console/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and make the ``name`` argument required::
{
// ...

protected function configure()
protected function configure(): void
{
$this
// ...
Expand All @@ -42,7 +42,7 @@ You now have access to a ``last_name`` argument in your command::
{
// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$text = 'Hi '.$input->getArgument('name');

Expand All @@ -52,6 +52,8 @@ You now have access to a ``last_name`` argument in your command::
}

$output->writeln($text.'!');

return 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion console/lockable_trait.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ that adds two convenient methods to lock and release commands::

// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->lock()) {
$output->writeln('The command is already running in another process.');
Expand Down
6 changes: 3 additions & 3 deletions console/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Consider for example the code used to display the title of the following command
{
// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln([
'<info>Lorem Ipsum Dolor Sit Amet</>',
Expand Down Expand Up @@ -62,7 +62,7 @@ title of the command::
{
// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Lorem Ipsum Dolor Sit Amet');
Expand Down Expand Up @@ -399,7 +399,7 @@ of your commands to change their appearance::
{
// ...

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Before
$io = new SymfonyStyle($input, $output);
Expand Down
4 changes: 3 additions & 1 deletion console/verbosity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ level. For example::
{
// ...

public function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
$user = new User(...);

Expand All @@ -68,6 +68,8 @@ level. For example::
'Will only be printed in verbose mode or higher',
OutputInterface::VERBOSITY_VERBOSE
);

return 0;
}
}

Expand Down
3 changes: 2 additions & 1 deletion mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ and create an :class:`Symfony\\Component\\Mime\\Email` object::
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

Expand All @@ -234,7 +235,7 @@ and create an :class:`Symfony\\Component\\Mime\\Email` object::
/**
* @Route("/email")
*/
public function sendEmail(MailerInterface $mailer)
public function sendEmail(MailerInterface $mailer): Response
{
$email = (new Email())
->from('[email protected]')
Expand Down
12 changes: 7 additions & 5 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ You can deny access from inside a controller::
// src/Controller/AdminController.php
// ...

public function adminDashboard()
public function adminDashboard(): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');

Expand Down Expand Up @@ -688,7 +688,7 @@ using annotations:
+ *
+ * @IsGranted("ROLE_ADMIN")
+ */
public function adminDashboard()
public function adminDashboard(): Response
{
// ...
}
Expand Down Expand Up @@ -735,7 +735,7 @@ role::

// ...

public function adminDashboard()
public function adminDashboard(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

Expand Down Expand Up @@ -770,7 +770,7 @@ like this:
After authentication, the ``User`` object of the current user can be accessed
via the ``getUser()`` shortcut::

public function index()
public function index(): Response
{
// usually you'll want to make sure the user is authenticated first
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
Expand Down Expand Up @@ -811,6 +811,8 @@ If you need to get the logged in user from a service, use the
{
// returns User object or null if not authenticated
$user = $this->security->getUser();

// ...
}
}

Expand Down Expand Up @@ -901,7 +903,7 @@ Next, you'll need to create a route for this URL (but not a controller):
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout()
public function logout(): void
{
// controller can be blank: it will never be executed!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
Expand Down
2 changes: 1 addition & 1 deletion security/access_denied_handler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ response)::

class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
public function handle(Request $request, AccessDeniedException $accessDeniedException)
public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
{
// ...

Expand Down
7 changes: 4 additions & 3 deletions security/csrf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ this can be customized on a form-by-form basis::

// src/Form/TaskType.php
namespace App\Form;

// ...
use App\Entity\Task;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -94,7 +94,7 @@ this can be customized on a form-by-form basis::
{
// ...

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Task::class,
Expand Down Expand Up @@ -154,9 +154,10 @@ Then, get the value of the CSRF token in the controller action and use the
to check its validity::

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// ...

public function delete(Request $request)
public function delete(Request $request): Response
{
$submittedToken = $request->request->get('token');

Expand Down
Loading