Skip to content

Commit

Permalink
Fix PhpStan.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 15, 2022
1 parent 9da7c4e commit 47aa953
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
],
"require": {
"php": "^8.0",
"baraja-core/lock": "^v1.0",
"baraja-core/lock": "^1.0",
"baraja-core/structured-api": "^4.0",
"baraja-core/network": "^1.0",
"nette/utils": "^3.2",
"doctrine/orm": "^2.9"
},
"require-dev": {
"baraja-core/dynamic-configuration": "^2.2",
"nette/di": "^3.0",
"phpstan/phpstan": "^1.0",
"phpstan/extension-installer": "^1.1",
Expand Down
21 changes: 0 additions & 21 deletions src/AuthenticationService.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Authenticator

public function __construct(
private EntityManagerInterface $entityManager,
private Configuration $configuration,
private UserStorage $userStorage,
private UserMetaManager $metaManager,
private ?Configuration $configuration = null,
) {
$organisationRepository = $entityManager->getRepository(Organisation::class);
assert($organisationRepository instanceof OrganisationRepository);
Expand Down Expand Up @@ -133,7 +133,7 @@ public function authentication(

public function isLoginFirewallBlocked(string $username, ?string $ip = null): bool
{
if (PHP_SAPI === 'cli') {
if (PHP_SAPI === 'cli' || $this->configuration === null) {
return false;
}
$blockCountKey = 'user-login-attempts-block-count';
Expand Down
10 changes: 7 additions & 3 deletions src/CasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Baraja\CAS\Service\PasswordAuthorizator;
use Baraja\CAS\Service\UserMetaManager;
use Baraja\Cms\CmsExtension;
use Baraja\Doctrine\ORM\DI\OrmAnnotationsExtension;
use Nette\DI\CompilerExtension;

Expand All @@ -18,14 +17,19 @@ final class CasExtension extends CompilerExtension
*/
public static function mustBeDefinedBefore(): array
{
return [OrmAnnotationsExtension::class, CmsExtension::class];
return [
'Baraja\Doctrine\ORM\DI\OrmAnnotationsExtension',
'Baraja\Cms\CmsExtension',
];
}


public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();
OrmAnnotationsExtension::addAnnotationPathToManager($builder, 'Baraja\CAS\Entity', __DIR__ . '/Entity');
if (class_exists(OrmAnnotationsExtension::class)) {
OrmAnnotationsExtension::addAnnotationPathToManager($builder, 'Baraja\CAS\Entity', __DIR__ . '/Entity');
}

$builder->addDefinition($this->prefix('user'))
->setFactory(User::class);
Expand Down
17 changes: 10 additions & 7 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,12 @@ public function setEmail(UserEmail $email): void
*/
public function getEmails(): array
{
return array_map(
static fn(UserEmail $email): string => $email->getEmail(),
$this->emails,
);
$return = [];
foreach ($this->emails as $email) {
$return[] = $email->getEmail();
}

return $return;
}


Expand Down Expand Up @@ -459,9 +461,10 @@ public function getPhone(): ?string

public function setPhone(?string $phone, int $region = 420): void
{
$this->phone = $phone !== null && $phone !== ''
? PhoneNumberFormatter::fix($phone, $region)
: null;
if (class_exists(PhoneNumberFormatter::class) && $phone !== null && $phone !== '') {
$phone = PhoneNumberFormatter::fix($phone, $region);
}
$this->phone = $phone;
}


Expand Down
9 changes: 7 additions & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ public static function hashPassword(string $password): string
}

$hash = @password_hash($password, PASSWORD_DEFAULT); // @ is escalated to exception
if (!$hash) {
throw new \LogicException('Computed hash is invalid. ' . error_get_last()['message']);
if ($hash === '') {
$error = error_get_last();
throw new \LogicException(
sprintf(
'Computed hash is invalid "%s".',
$error !== null ? $error['message'] : ''),
);
}

return $hash;
Expand Down
1 change: 0 additions & 1 deletion src/Service/UserMetaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function set(int $userId, string $key, ?string $value): self
$userMetaRepository = $this->entityManager->getRepository(UserMeta::class);
assert($userMetaRepository instanceof UserMetaRepository);
$meta = self::$cache[$cacheKey] ?? $userMetaRepository->load($user->getId(), $key);
assert($meta instanceof UserMeta);
} catch (NoResultException | NonUniqueResultException) {
if ($value === null) {
return $this;
Expand Down

0 comments on commit 47aa953

Please sign in to comment.