Skip to content

Commit

Permalink
SA: Fix Static Analysis issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 24, 2022
1 parent e5caf3e commit c00d444
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 52 deletions.
23 changes: 23 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

use drupol\PhpCsFixerConfigsPhp\Config\Php73;

$config = new Php73();

$config
->getFinder()
->ignoreDotFiles(false)
->name(['.php-cs-fixer.dist.php']);

$rules = $config->getRules();

$rules['return_assignment'] = false;

return $config->setRules($rules);
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"roave/you-are-using-it-wrong": "^1"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.10",
"doctrine/doctrine-bundle": "^2.6",
"doctrine/orm": "^2.12",
"drupol/php-conventions": "^5",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.5"
Expand Down
33 changes: 19 additions & 14 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ imports:
- { resource: vendor/drupol/php-conventions/config/php73/grumphp.yml }

parameters:
extra_tasks:
phpunit: ~

# GrumPHP License
tasks.license.holder: Pol Dellaiera
tasks.license.date_from: 2021

# PHP CS Fixer
tasks.phpcsfixer.config: .php-cs-fixer.dist.php
tasks.phpcsfixer.diff: true

# PHPCS
tasks.phpcs.ignore_patterns:
- "/.github/"
Expand All @@ -27,18 +28,18 @@ parameters:
tasks.phpstan.level: 8
tasks.phpstan.blocking: true
tasks.phpstan.ignore_patterns:
- ".github/"
- ".idea/"
- "build/"
- "benchmarks/"
- "docs/"
- "node_modules/"
- "resource/"
- "spec/"
- "var/"
- "vendor/"
- "tests/App/"
- "/.github/"
- "/.idea/"
- "/build/"
- "/benchmarks/"
- "/docs/"
- "/node_modules/"
- "/resource/"
- "/spec/"
- "/var/"
- "/vendor/"
- "tests/unit/"
- "tests/src/"

# Psalm
tasks.psalm.blocking: true
Expand All @@ -52,3 +53,7 @@ parameters:
- "/spec/"
- "/var/"
- "/vendor/"

extra_tasks:
phpunit:
always_execute: true
8 changes: 6 additions & 2 deletions src/Doctrine/MonadicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Throwable;

/**
* @template R
* @template R of object
*
* @implements MonadicRepositoryInterface<Throwable, R>
*/
Expand All @@ -41,7 +41,11 @@ public function __construct(
string $entityClass
) {
$this->objectRepository = $entityManager->getRepository($entityClass);
$this->monadicServiceRepositoryHelper = new MonadicServiceRepositoryHelper();

/** @var MonadicServiceRepositoryHelperInterface<Throwable, R> $monadicServiceRepositoryHelper */
$monadicServiceRepositoryHelper = new MonadicServiceRepositoryHelper();

$this->monadicServiceRepositoryHelper = $monadicServiceRepositoryHelper;
}

public function eitherFind($id): Either
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/MonadicRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
namespace loophp\RepositoryMonadicHelper\Doctrine;

use Marcosh\LamPHPda\Either;
use Throwable;

/**
* @template L
* @template R
* @template L of Throwable
* @template R of object
*/
interface MonadicRepositoryInterface
{
Expand Down
7 changes: 5 additions & 2 deletions src/Doctrine/MonadicServiceEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Throwable;

/**
* @template R
* @template R of object
*
* @implements MonadicServiceEntityRepositoryInterface<Throwable, R>
* @extends ServiceEntityRepository<R>
Expand Down Expand Up @@ -52,6 +52,9 @@ public function eitherFindOneBy(array $criteria): Either
*/
private function getMonadicServiceRepositoryHelper(): MonadicServiceRepositoryHelperInterface
{
return new MonadicServiceRepositoryHelper();
/** @var MonadicServiceRepositoryHelperInterface<Throwable, R> $monadicServiceRepositoryHelper */
$monadicServiceRepositoryHelper = new MonadicServiceRepositoryHelper();

return $monadicServiceRepositoryHelper;
}
}
2 changes: 1 addition & 1 deletion src/Doctrine/MonadicServiceEntityRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* @template L
* @template R
* @template R of object
*
* @extends ObjectRepository<R>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/MonadicRepositoryFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
interface MonadicRepositoryFactoryInterface
{
/**
* @template R
* @template R of object
*
* @param class-string<R> $entityClass
*
Expand All @@ -25,7 +25,7 @@ interface MonadicRepositoryFactoryInterface
public function fromEntity(string $entityClass): MonadicRepositoryInterface;

/**
* @template R
* @template R of object
*
* @param ObjectRepository<R> $objectRepository
*
Expand Down
63 changes: 45 additions & 18 deletions src/Service/MonadicServiceRepositoryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,47 @@
use Throwable;

/**
* @template R
* @template L of Throwable
* @template R of object
*
* @implements MonadicServiceRepositoryHelperInterface<Throwable, R>
* @implements MonadicServiceRepositoryHelperInterface<L, R>
*/
final class MonadicServiceRepositoryHelper implements MonadicServiceRepositoryHelperInterface
{
public function eitherFind(ObjectRepository $objectRepository, $id): Either
{
return (null === $entity = $objectRepository->find($id))
? Either::left(
if (null === $entity = $objectRepository->find($id)) {
/** @var Either<L, R> $either */
$either = Either::left(
MonadicRepositoryException::entityNotFoundWithSuchId(
$objectRepository->getClassName(),
(string) $id
)
)
: Either::right($entity);
);

return $either;
}

return Either::right($entity);
}

public function eitherFindAll(ObjectRepository $objectRepository): Either
{
return ([] === $entities = $objectRepository->findAll())
? Either::left(
if ([] === $entities = $objectRepository->findAll()) {
/** @var Either<L, list<R>> $either */
$either = Either::left(
MonadicRepositoryException::entityNotFound(
$objectRepository->getClassName()
)
)
: Either::right($entities);
);

return $either;
}

/** @var Either<L, list<R>> $either */
$either = Either::right($entities);

return $either;
}

public function eitherFindBy(
Expand All @@ -51,25 +65,38 @@ public function eitherFindBy(
?int $limit = null,
?int $offset = null
): Either {
return ([] === $entities = $objectRepository->findBy($criteria, $orderBy, $limit, $offset))
? Either::left(
if ([] === $entities = $objectRepository->findBy($criteria, $orderBy, $limit, $offset)) {
/** @var Either<L, list<R>> $either */
$either = Either::left(
MonadicRepositoryException::entityNotFoundWithSuchCriteria(
$objectRepository->getClassName(),
$criteria
)
)
: Either::right($entities);
);

return $either;
}

/** @var Either<L, list<R>> $either */
$either = Either::right($entities);

return $either;
}

public function eitherFindOneBy(ObjectRepository $objectRepository, array $criteria): Either
{
return (null === $entity = $objectRepository->findOneBy($criteria))
? Either::left(
if (null === $entity = $objectRepository->findOneBy($criteria)) {
/** @var Either<L, R> $either */
$either = Either::left(
MonadicRepositoryException::entityNotFoundWithSuchCriteria(
$objectRepository->getClassName(),
$criteria
)
)
: Either::right($entity);
);

return $either;
}

return Either::right($entity);
}
}
2 changes: 1 addition & 1 deletion src/Service/MonadicServiceRepositoryHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* @template L
* @template R
* @template R of object
*/
interface MonadicServiceRepositoryHelperInterface
{
Expand Down
28 changes: 24 additions & 4 deletions tests/App/Repository/BareCustomEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,43 @@
*/
class BareCustomEntityRepository implements ObjectRepository
{
public function find($id)
public function find($id): ?CustomEntity
{
if (mt_rand(0, 1)) {
return null;
}

return new CustomEntity();
}

public function findAll()
/**
* @return CustomEntity[]
*/
public function findAll(): array
{
return [new CustomEntity()];
}

public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null)
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
if (mt_rand(0, 1)) {
return [];
}

return [new CustomEntity()];
}

public function findOneBy(array $criteria)
public function findOneBy(array $criteria): ?CustomEntity
{
if (mt_rand(0, 1)) {
return null;
}

return new CustomEntity();
}

public function getClassName()
{
return CustomEntity::class;
}
}
28 changes: 24 additions & 4 deletions tests/App/Repository/MonadicBareCustomEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,43 @@ class MonadicBareCustomEntityRepository implements MonadicServiceEntityRepositor
{
use MonadicServiceEntityRepositoryTrait;

public function find($id)
public function find($id): ?CustomEntity
{
if (mt_rand(0, 1)) {
return null;
}

return new CustomEntity();
}

public function findAll()
/**
* @return CustomEntity[]
*/
public function findAll(): array
{
return [new CustomEntity()];
}

public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null)
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
if (mt_rand(0, 1)) {
return [];
}

return [new CustomEntity()];
}

public function findOneBy(array $criteria)
public function findOneBy(array $criteria): ?CustomEntity
{
if (mt_rand(0, 1)) {
return null;
}

return new CustomEntity();
}

public function getClassName()
{
return CustomEntity::class;
}
}

0 comments on commit c00d444

Please sign in to comment.