Skip to content

Commit b718c4f

Browse files
committed
feature #853 [make:voter] generate type hints (jrushlow)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- [make:voter] generate type hints starting in Symfony 5.0 the abstract voter methods introduced type hints (symfony/symfony@8c46b95). We now conditionally generate the `string` type hint for the `$attribute` argument. Minor cleanup of the maker itself. Commits ------- 95980dc [make:voter] generate type hints
2 parents 5387922 + 95980dc commit b718c4f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Maker/MakeVoter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Command\Command;
1919
use Symfony\Component\Console\Input\InputArgument;
2020
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\HttpKernel\Kernel;
2122
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
2223

2324
/**
@@ -36,15 +37,15 @@ public static function getCommandDescription(): string
3637
return 'Creates a new security voter class';
3738
}
3839

39-
public function configureCommand(Command $command, InputConfiguration $inputConf)
40+
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
4041
{
4142
$command
4243
->addArgument('name', InputArgument::OPTIONAL, 'The name of the security voter class (e.g. <fg=yellow>BlogPostVoter</>)')
4344
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeVoter.txt'))
4445
;
4546
}
4647

47-
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
48+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
4849
{
4950
$voterClassNameDetails = $generator->createClassNameDetails(
5051
$input->getArgument('name'),
@@ -55,7 +56,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5556
$generator->generateClass(
5657
$voterClassNameDetails->getFullName(),
5758
'security/Voter.tpl.php',
58-
[]
59+
[
60+
'use_type_hints' => 50000 <= Kernel::VERSION_ID,
61+
]
5962
);
6063

6164
$generator->writeChanges();
@@ -68,7 +71,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6871
]);
6972
}
7073

71-
public function configureDependencies(DependencyBuilder $dependencies)
74+
public function configureDependencies(DependencyBuilder $dependencies): void
7275
{
7376
$dependencies->addClassDependency(
7477
Voter::class,

src/Resources/skeleton/security/Voter.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
class <?= $class_name ?> extends Voter
1010
{
11-
protected function supports($attribute, $subject)
11+
protected function supports(<?= $use_type_hints ? 'string ' : null ?>$attribute, $subject): bool
1212
{
1313
// replace with your own logic
1414
// https://symfony.com/doc/current/security/voters.html
1515
return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
1616
&& $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_name) ?>;
1717
}
1818

19-
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
19+
protected function voteOnAttribute(<?= $use_type_hints ? 'string ' : null ?>$attribute, $subject, TokenInterface $token): bool
2020
{
2121
$user = $token->getUser();
2222
// if the user is anonymous, do not grant access

0 commit comments

Comments
 (0)