Skip to content
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

[make:voter] generate type hints #853

Merged
merged 1 commit into from
Mar 30, 2021
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
11 changes: 7 additions & 4 deletions src/Maker/MakeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

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

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

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$voterClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
Expand All @@ -55,7 +56,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$generator->generateClass(
$voterClassNameDetails->getFullName(),
'security/Voter.tpl.php',
[]
[
'use_type_hints' => 50000 <= Kernel::VERSION_ID,
]
);

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

public function configureDependencies(DependencyBuilder $dependencies)
public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(
Voter::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/skeleton/security/Voter.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

class <?= $class_name ?> extends Voter
{
protected function supports($attribute, $subject)
protected function supports(<?= $use_type_hints ? 'string ' : null ?>$attribute, $subject): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have this conversation on every PR :p. Would generating the types break if I were using Symfony 4.4, where the parent class does NOT have those hints? If so, then we should do this - but only if we're on Symfony 5.

Copy link
Collaborator Author

@jrushlow jrushlow Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - i keep forgetting how this works myself, but if the abstract does not declare a return type, the child is able to declare one. For arguments it would break iirc...

// https://www.php.net/manual/en/language.types.declarations.php

Note:

When overriding a parent method, the child's method must match any return type declaration on the parent. If the parent doesn't define a return type, then the child method may do so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, then we should do this - but only if we're on Symfony 5.

The new $use_type_hints var checks if we're using symfony 5 or greater (double check my comparison operator :D) for the arg. But this check isnt needed for the return type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - i keep forgetting how this works myself, but if the abstract does not declare a return type, the child is able to declare one. For arguments it would break iirc...

Ah yes. It's legal to make a return type "more restrictive" than your parent, but it is NOT legal to make an argument type stricter (which would now mean you accept LESS input)

{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
&& $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_name) ?>;
}

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