Skip to content

Commit

Permalink
fix: Reached phpstan level 7 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 27, 2023
1 parent a6188c6 commit d5c0bdc
Show file tree
Hide file tree
Showing 35 changed files with 281 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __invoke(NodesSources $source): array
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
return iterator_to_array($iterator);
}
return $children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __invoke(NodesSources $source): array
if ($iterator instanceof ArrayIterator) {
return $iterator->getArrayCopy();
}
return iterator_to_array($iterator);
}
return $children;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public function getTreeWalkersForTypeAtRoot(
->camel()
->toString();

/** @var callable $callable */
$callable = [$walkerClass, 'build'];
$walkers[$walkerName] = call_user_func(
[$walkerClass, 'build'],
$callable,
$root,
$this->walkerContext,
$maxLevel,
Expand Down
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/src/Console/FilesImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$appNamespace = (new AsciiSlugger())->slug($this->appNamespace, '_');
$tempDir = tempnam(sys_get_temp_dir(), $appNamespace . '_files');
if (false === $tempDir) {
throw new \RuntimeException('Cannot create temporary directory.');
}
if (file_exists($tempDir)) {
unlink($tempDir);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/RoadizCoreBundle/src/Console/LogsCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RZ\Roadiz\CoreBundle\Entity\Log;
use RZ\Roadiz\CoreBundle\Repository\LogRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -47,7 +48,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (\is_string($input->getOption('since'))) {
$since = '-' . $input->getOption('since');
}
$now->add(\DateInterval::createFromDateString($since));
$interval = \DateInterval::createFromDateString($since);
if (false === $interval) {
throw new InvalidArgumentException('Invalid since option format.');
}
$now->add($interval);
$io = new SymfonyStyle($input, $output);

/** @var LogRepository $logRepository */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ private function executeCreation(InputInterface $input, OutputInterface $output)
protected function addNodeTypeField(NodeType $nodeType, int|float|string $position, SymfonyStyle $io): void
{
$field = new NodeTypeField();
$field->setPosition((float) $position);
$position = floatval($position);
$field->setPosition($position);

$questionfName = new Question('[Field ' . $position . '] <question>Enter field name</question>', 'content');
$fName = $io->askQuestion($questionfName);
Expand Down
72 changes: 43 additions & 29 deletions lib/RoadizCoreBundle/src/Console/UsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RZ\Roadiz\CoreBundle\Entity\Role;
use RZ\Roadiz\CoreBundle\Entity\User;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -20,12 +21,9 @@ class UsersCommand extends Command
{
protected ManagerRegistry $managerRegistry;

/**
* @param ManagerRegistry $managerRegistry
*/
public function __construct(ManagerRegistry $managerRegistry)
public function __construct(ManagerRegistry $managerRegistry, string $name = null)
{
parent::__construct();
parent::__construct($name);
$this->managerRegistry = $managerRegistry;
}

Expand All @@ -40,6 +38,19 @@ protected function configure(): void
);
}

protected function getUserTableRow(User $user): array
{
return [
'Id' => $user->getId(),
'Username' => $user->getUsername(),
'Email' => $user->getEmail(),
'Disabled' => (!$user->isEnabled() ? 'X' : ''),
'Expired' => ($user->getExpired() ? 'X' : ''),
'Locked' => (!$user->isAccountNonLocked() ? 'X' : ''),
'Groups' => implode(' ', $user->getGroupNames()),
];
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand All @@ -54,18 +65,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($user === null) {
$io->error('User “' . $name . '” does not exist… use users:create to add a new user.');
} else {
$tableContent = [[
$user->getId(),
$user->getUsername(),
$user->getEmail(),
(!$user->isEnabled() ? 'X' : ''),
($user->getExpired() ? 'X' : ''),
(!$user->isAccountNonLocked() ? 'X' : ''),
implode(' ', $user->getGroupNames()),
implode(' ', $user->getRoles()),
]];
$tableContent = [
$this->getUserTableRow($user),
];
$io->table(
['Id', 'Username', 'Email', 'Disabled', 'Expired', 'Locked', 'Groups', 'Roles'],
array_keys($tableContent[0]),
$tableContent
);
}
Expand All @@ -77,20 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (count($users) > 0) {
$tableContent = [];
foreach ($users as $user) {
$tableContent[] = [
$user->getId(),
$user->getUsername(),
$user->getEmail(),
(!$user->isEnabled() ? 'X' : ''),
($user->getExpired() ? 'X' : ''),
(!$user->isAccountNonLocked() ? 'X' : ''),
implode(' ', $user->getGroupNames()),
implode(' ', $user->getRoles()),
];
$tableContent[] = $this->getUserTableRow($user);
}

$io->table(
['Id', 'Username', 'Email', 'Disabled', 'Expired', 'Locked', 'Groups', 'Roles'],
array_keys($tableContent[0]),
$tableContent
);
} else {
Expand All @@ -100,14 +95,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

protected function getUserForInput(InputInterface $input): User
{
$name = $input->getArgument('username');

if (!\is_string($name) || empty($name)) {
throw new InvalidArgumentException('Username argument is required.');
}

/** @var User|null $user */
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if (!($user instanceof User)) {
throw new InvalidArgumentException('User “' . $name . '” does not exist.');
}

return $user;
}

/**
* Get role by name, and create it if it does not exist.
*
* @param string $roleName
*
* @return Role
*/
public function getRole(string $roleName = Role::ROLE_SUPERADMIN)
public function getRole(string $roleName = Role::ROLE_SUPERADMIN): Role
{
$role = $this->managerRegistry
->getRepository(Role::class)
Expand Down
43 changes: 23 additions & 20 deletions lib/RoadizCoreBundle/src/Console/UsersCreationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,32 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('username');

if ($name) {
/** @var User|null $user */
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if (null === $user) {
$user = $this->executeUserCreation($name, $input, $output);

// Change password right away
$command = $this->getApplication()->find('users:password');
$arguments = [
'username' => $user->getUsername(),
];
$passwordInput = new ArrayInput($arguments);
return $command->run($passwordInput, $output);
} else {
throw new \InvalidArgumentException('User “' . $name . '” already exists.');
}
if (!\is_string($name) || empty($name)) {
throw new \InvalidArgumentException('Username argument is required.');
}
return 0;

/** @var User|null $user */
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if ($user instanceof User) {
$io->warning('User “' . $name . '” already exists.');
return 1;
}

$user = $this->executeUserCreation($name, $input, $output);

// Change password right away
$command = $this->getApplication()->find('users:password');
$arguments = [
'username' => $user->getUsername(),
];
$passwordInput = new ArrayInput($arguments);
return $command->run($passwordInput, $output);
}

/**
Expand Down
43 changes: 17 additions & 26 deletions lib/RoadizCoreBundle/src/Console/UsersDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('username');
$user = $this->getUserForInput($input);

if ($name) {
/** @var User|null $user */
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if (null !== $user) {
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to delete user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$this->managerRegistry->getManagerForClass(User::class)->remove($user);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” deleted.');
} else {
$io->warning('User “' . $name . '” was not deleted.');
}
} else {
throw new \InvalidArgumentException('User “' . $name . '” does not exist.');
}
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to delete user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$this->managerRegistry->getManagerForClass(User::class)->remove($user);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” deleted.');
return 0;
} else {
$io->warning('User “' . $name . '” was not deleted.');
return 1;
}
return 0;
}
}
43 changes: 17 additions & 26 deletions lib/RoadizCoreBundle/src/Console/UsersDisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('username');
$user = $this->getUserForInput($input);

if ($name) {
/** @var User|null $user */
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if (null !== $user) {
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to disable user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$user->setEnabled(false);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” disabled.');
} else {
$io->warning('User “' . $name . '” was not disabled.');
}
} else {
throw new \InvalidArgumentException('User “' . $name . '” does not exist.');
}
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to disable user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$user->setEnabled(false);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” disabled.');
return 0;
} else {
$io->warning('User “' . $name . '” was not disabled.');
return 1;
}
return 0;
}
}
42 changes: 17 additions & 25 deletions lib/RoadizCoreBundle/src/Console/UsersEnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('username');
$user = $this->getUserForInput($input);

if ($name) {
$user = $this->managerRegistry
->getRepository(User::class)
->findOneBy(['username' => $name]);

if (null !== $user) {
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to enable user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$user->setEnabled(true);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” was enabled.');
} else {
$io->warning('User “' . $name . '” was not enabled');
}
} else {
throw new \InvalidArgumentException('User “' . $name . '” does not exist.');
}
$confirmation = new ConfirmationQuestion(
'<question>Do you really want to enable user “' . $user->getUsername() . '”?</question>',
false
);
if (
!$input->isInteractive() || $io->askQuestion(
$confirmation
)
) {
$user->setEnabled(true);
$this->managerRegistry->getManagerForClass(User::class)->flush();
$io->success('User “' . $name . '” was enabled.');
return 0;
} else {
$io->warning('User “' . $name . '” was not enabled');
return 1;
}
return 0;
}
}
Loading

0 comments on commit d5c0bdc

Please sign in to comment.