Skip to content

Commit

Permalink
refactor: Use friendsofphp/php-cs-fixer instead of squizlabs/php_code…
Browse files Browse the repository at this point in the history
…sniffer (#22)
  • Loading branch information
roadiz-ci committed Nov 6, 2024
1 parent 345f409 commit bc95777
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 137 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,5 @@ jobs:
${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install Dependencies
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,5 @@ modules.xml
/lib/
/.data/

###> squizlabs/php_codesniffer ###
/.phpcs-cache
/phpcs.xml
###< squizlabs/php_codesniffer ###
/report.txt
/composer.lock
3 changes: 0 additions & 3 deletions Makefile

This file was deleted.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"roadiz/doc-generator": "2.4.x-dev",
"roadiz/entity-generator": "2.4.x-dev",
"roadiz/jwt": "2.4.x-dev",
"roadiz/random": "2.4.x-dev",
"squizlabs/php_codesniffer": "^3.5"
"roadiz/random": "2.4.x-dev"
},
"config": {
"optimize-autoloader": true,
Expand Down
13 changes: 0 additions & 13 deletions phpcs.xml.dist

This file was deleted.

3 changes: 2 additions & 1 deletion src/Console/PurgeUserValidationTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

final class PurgeUserValidationTokenCommand extends Command
{
public function __construct(private readonly ManagerRegistry $managerRegistry, string $name = null)
public function __construct(private readonly ManagerRegistry $managerRegistry, ?string $name = null)
{
parent::__construct($name);
}
Expand All @@ -32,6 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$count = $this->managerRegistry->getRepository(UserValidationToken::class)->deleteAllExpired();

$io->success(sprintf('%d expired user validation token(s) were deleted.', $count));

return 0;
}
}
20 changes: 5 additions & 15 deletions src/DependencyInjection/Compiler/DoctrineMigrationCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace RZ\Roadiz\UserBundle\DependencyInjection\Compiler;

use RuntimeException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DoctrineMigrationCompilerPass implements CompilerPassInterface
{
/**
* @inheritDoc
*/
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('doctrine.migrations.configuration')) {
Expand All @@ -27,13 +23,13 @@ public function process(ContainerBuilder $container): void

private function checkIfBundleRelativePath(string $path, ContainerBuilder $container): string
{
if (isset($path[0]) && $path[0] === '@') {
$pathParts = explode('/', $path);
if (isset($path[0]) && '@' === $path[0]) {
$pathParts = explode('/', $path);
$bundleName = \mb_substr($pathParts[0], 1);

$bundlePath = $this->getBundlePath($bundleName, $container);

return $bundlePath . \mb_substr($path, \mb_strlen('@' . $bundleName));
return $bundlePath.\mb_substr($path, \mb_strlen('@'.$bundleName));
}

return $path;
Expand All @@ -44,14 +40,8 @@ private function getBundlePath(string $bundleName, ContainerBuilder $container):
$bundleMetadata = $container->getParameter('kernel.bundles_metadata');
assert(is_array($bundleMetadata));

if (! isset($bundleMetadata[$bundleName])) {
throw new RuntimeException(
sprintf(
'The bundle "%s" has not been registered, available bundles: %s',
$bundleName,
implode(', ', array_keys($bundleMetadata))
)
);
if (!isset($bundleMetadata[$bundleName])) {
throw new \RuntimeException(sprintf('The bundle "%s" has not been registered, available bundles: %s', $bundleName, implode(', ', array_keys($bundleMetadata))));
}

return $bundleMetadata[$bundleName]['path'];
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('email_validated_role_name')
->defaultValue('ROLE_EMAIL_VALIDATED')
->end();

return $builder;
}
}
5 changes: 1 addition & 4 deletions src/DependencyInjection/RoadizUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

class RoadizUserExtension extends Extension
{
/**
* @inheritDoc
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/../config'));
$loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__).'/../config'));
$loader->load('services.yaml');

$configuration = new Configuration();
Expand Down
24 changes: 3 additions & 21 deletions src/Entity/UserMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,39 @@ class UserMetadata
#[ORM\Column(name: 'metadata', type: 'json', nullable: true)]
private ?array $metadata = [];

/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}

/**
* @param int $id
* @return UserMetadata
*/
public function setId(int $id): UserMetadata
{
$this->id = $id;

return $this;
}

/**
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}

/**
* @param User|null $user
* @return UserMetadata
*/
public function setUser(?User $user): UserMetadata
{
$this->user = $user;

return $this;
}

/**
* @return array|null
*/
public function getMetadata(): ?array
{
return $this->metadata;
}

/**
* @param array|null $metadata
* @return UserMetadata
*/
public function setMetadata(?array $metadata): UserMetadata
{
$this->metadata = $metadata;

return $this;
}
}
33 changes: 4 additions & 29 deletions src/Entity/UserValidationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,76 +33,51 @@ class UserValidationToken
#[ORM\Column(name: 'token_valid_until', type: 'datetime', unique: false, nullable: true)]
private ?\DateTime $tokenValidUntil = null;

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* @param int $id
* @return UserValidationToken
*/
public function setId(int $id): UserValidationToken
{
$this->id = $id;

return $this;
}


/**
* @return UserInterface|null
*/
public function getUser(): ?UserInterface
{
return $this->user;
}

/**
* @param UserInterface|null $user
* @return UserValidationToken
*/
public function setUser(?UserInterface $user): UserValidationToken
{
$this->user = $user;

return $this;
}

/**
* @return string
*/
public function getToken(): string
{
return $this->token;
}

/**
* @param string $token
* @return UserValidationToken
*/
public function setToken(string $token): UserValidationToken
{
$this->token = $token;

return $this;
}

/**
* @return \DateTime|null
*/
public function getTokenValidUntil(): ?\DateTime
{
return $this->tokenValidUntil;
}

/**
* @param \DateTime|null $tokenValidUntil
* @return UserValidationToken
*/
public function setTokenValidUntil(?\DateTime $tokenValidUntil): UserValidationToken
{
$this->tokenValidUntil = $tokenValidUntil;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public function __construct(
) {
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
Expand Down
7 changes: 2 additions & 5 deletions src/EventSubscriber/PasswordlessUserSignedUpSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
final readonly class PasswordlessUserSignedUpSubscriber implements EventSubscriberInterface
{
public function __construct(
private UserValidationTokenManagerInterface $userValidationTokenManager
private UserValidationTokenManagerInterface $userValidationTokenManager,
) {
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
PasswordlessUserSignedUp::class => 'onUserSignedUp'
PasswordlessUserSignedUp::class => 'onUserSignedUp',
];
}

Expand Down
7 changes: 2 additions & 5 deletions src/EventSubscriber/UserSignedUpSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
final class UserSignedUpSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly UserValidationTokenManagerInterface $userValidationTokenManager
private readonly UserValidationTokenManagerInterface $userValidationTokenManager,
) {
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
UserSignedUp::class => 'onUserSignedUp'
UserSignedUp::class => 'onUserSignedUp',
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Manager/UserMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function createMetadataForUser(User $user): UserMetadata
$userMetadata = new UserMetadata();
$userMetadata->setUser($user);
$this->managerRegistry->getManager()->persist($userMetadata);

return $userMetadata;
}
}
1 change: 1 addition & 0 deletions src/Manager/UserMetadataManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
interface UserMetadataManagerInterface
{
public function getMetadataForUser(User $user): ?UserMetadata;

public function createMetadataForUser(User $user): UserMetadata;
}
13 changes: 7 additions & 6 deletions src/Manager/UserValidationTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
private RoleHierarchyInterface $roleHierarchy,
private string $emailValidatedRoleName,
private int $userValidationExpiresIn,
private string $userValidationUrl
private string $userValidationUrl,
) {
}

Expand All @@ -54,17 +54,18 @@ public function createForUser(UserInterface $user, bool $sendEmail = true): User
if ($sendEmail) {
$this->sendUserValidationEmail($existingValidationToken);
}

return $existingValidationToken;
}

public function isUserEmailValidated(UserInterface $user): bool
{
$reachableRoles = $this->roleHierarchy->getReachableRoleNames($user->getRoles());
return \in_array($this->emailValidatedRoleName, $reachableRoles) ||
\in_array('ROLE_SUPER_ADMIN', $reachableRoles) ||
\in_array('ROLE_SUPERADMIN', $reachableRoles);
}

return \in_array($this->emailValidatedRoleName, $reachableRoles)
|| \in_array('ROLE_SUPER_ADMIN', $reachableRoles)
|| \in_array('ROLE_SUPERADMIN', $reachableRoles);
}

private function sendUserValidationEmail(UserValidationToken $userValidationToken): void
{
Expand Down Expand Up @@ -92,7 +93,7 @@ private function sendUserValidationEmail(UserValidationToken $userValidationToke
UrlGeneratorInterface::ABSOLUTE_URL
);
} catch (RouteNotFoundException $exception) {
$validationLink = $this->userValidationUrl . '?' . http_build_query(
$validationLink = $this->userValidationUrl.'?'.http_build_query(
[
'token' => $userValidationToken->getToken(),
'_locale' => $user->getLocale(),
Expand Down
1 change: 1 addition & 0 deletions src/Manager/UserValidationTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
interface UserValidationTokenManagerInterface
{
public function createForUser(UserInterface $user, bool $sendEmail = true): UserValidationToken;

public function isUserEmailValidated(UserInterface $user): bool;
}
1 change: 1 addition & 0 deletions src/Repository/UserValidationTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function deleteAllExpired(): int
public function findOneByValidToken(string $token): ?UserValidationToken
{
$qb = $this->createQueryBuilder('t');

return $qb->andWhere($qb->expr()->eq('t.token', ':token'))
->andWhere($qb->expr()->gte('t.tokenValidUntil', ':now'))
->setParameter('token', $token)
Expand Down
Loading

0 comments on commit bc95777

Please sign in to comment.