Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.
Open
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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"autoload": {
"psr-4": {
"Libero\\ContentApiBundle\\": "src/"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"files": [
Expand Down Expand Up @@ -35,6 +38,7 @@
"php-vfs/php-vfs": "^1.4",
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-phpunit": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpunit/phpunit": "^7.2",
"symfony/console": "^3.4 || ^4.0",
"symfony/filesystem": "^3.4 || ^4.0",
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
parameters:
ignoreErrors:
- '~^.*(?=.* Symfony\\Component\\Config\\Definition\\Builder)(?=.*( |:{2})end\(\)).*$~'
- '~^Call to method PHPUnit\\Framework\\Assert::assertInstanceOf\(\) with .+? and .+? will always evaluate to true\.$~'
- '~^Dynamic call to static method PHPUnit\\Framework\\Assert::assert[A-z]+\(\)\.$~'

Choose a reason for hiding this comment

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

surely there must be a way to disable/enable rules selectively like https://github.com/phpstan/phpstan-strict-rules#enabling-rules-one-by-one?

level: max
paths:
- bin/console
Expand All @@ -10,3 +12,4 @@ parameters:
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
21 changes: 16 additions & 5 deletions src/Adapter/DoctrineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Libero\ContentApiBundle\Adapter;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\ParameterType;
Expand All @@ -24,15 +25,21 @@
use function array_map;
use function array_pop;
use function count;
use function is_string;
use function rewind;
use function sprintf;

final class DoctrineItems implements IteratorAggregate, Items
{
private const TRAVERSING_LIMIT = 100;

/** @var Connection */
private $connection;

/** @var string */
private $tableItems;

/** @var string */
private $tableVersions;

public function __construct(Connection $connection, string $tablePrefix)
Expand Down Expand Up @@ -147,8 +154,11 @@ public function get(ItemId $id, ?ItemVersionNumber $version = null) : ItemVersio

public function list(int $limit = 10, ?string $cursor = null) : ItemListPage
{
if (null !== $cursor && ((string) (int) $cursor) !== $cursor) {
return new ItemListPage([], null);
if (is_string($cursor)) {
$intCursor = (int) $cursor;
if ((string) $intCursor !== $cursor) {
return new ItemListPage([], null);
}
}

$query = $this->connection->createQueryBuilder();
Expand All @@ -160,16 +170,17 @@ public function list(int $limit = 10, ?string $cursor = null) : ItemListPage
->orderBy('item.sequence')
->setMaxResults($limit + 1);

if ($cursor) {
if (isset($intCursor)) {
$query
->where($query->expr()->gte('item.sequence', ':cursor'))
->setParameter('cursor', (int) $cursor);
->setParameter('cursor', $intCursor);
}

/** @var Statement $results */
$results = $query->execute();
$ids = $results->fetchAll(FetchMode::ASSOCIATIVE);

if (empty($ids)) {
if ([] === $ids) {
return new ItemListPage([], null);
}

Expand Down
22 changes: 18 additions & 4 deletions src/Adapter/FilesystemItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
use function iterator_to_array;
use function md5_file;
use function rsort;
use function stream_get_contents;
use function substr;

final class FilesystemItems implements IteratorAggregate, Items
{
/** @var Filesystem */
private $filesystem;

/** @var string */
private $path;

public function __construct(string $path, Filesystem $filesystem)
Expand All @@ -54,13 +58,19 @@ public function add(ItemVersion $item) : void
$current = null;
}

$next = $current ? $current->getVersion()->next() : ItemVersionNumber::fromInt(1);
$next = $current instanceof ItemVersion ? $current->getVersion()->next() : ItemVersionNumber::fromInt(1);

if ($version > $next) {
throw new UnexpectedVersionNumber($id, $version, $next);
}

$this->filesystem->dumpFile("{$this->path}/{$id}/{$version->toInt()}.xml", $item->getContent());
$contents = stream_get_contents($item->getContent());
Copy link
Contributor Author

Choose a reason for hiding this comment

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


if (false === $contents) {
throw new RuntimeException('Failed to get contents');
}

$this->filesystem->dumpFile("{$this->path}/{$id}/{$version->toInt()}.xml", $contents);
}

public function remove(ItemId $id, ?ItemVersionNumber $version) : void
Expand Down Expand Up @@ -104,15 +114,19 @@ public function get(ItemId $id, ?ItemVersionNumber $version = null) : ItemVersio
$version = $this->getVersions($id)->current();
}

if (!$content = @fopen($file = "{$this->path}/{$id}/{$version->toInt()}.xml", 'rb')) {
$content = @fopen($file = "{$this->path}/{$id}/{$version->toInt()}.xml", 'rb');

Choose a reason for hiding this comment

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

the @ operator is really what sticks out in this line more than the condition/assignment mixing


if (false === $content) {
if (!file_exists($file)) {
throw new VersionNotFound($id, $version);
} else {
throw new RuntimeException("Unable to open {$file}");
}
}

if (!$hash = md5_file($file)) {
$hash = md5_file($file);

if (false === $hash) {
throw new RuntimeException("Failed to hash {$file}");
}

Expand Down
4 changes: 1 addition & 3 deletions src/Adapter/InMemoryItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ public function list(int $limit = 10, ?string $cursor = null) : ItemListPage
$ids = array_keys($this->items);

if (null !== $cursor) {
/** @var int|false $offset */
$offset = array_search($cursor, $ids);
$offset = array_search($cursor, $ids, true);

if (false === $offset) {
return new ItemListPage([], null);
}
}

/** @var string[] $slice */
$slice = array_slice($ids, $offset ?? 0, $limit + 1);

if (count($slice) > $limit) {
Expand Down
1 change: 1 addition & 0 deletions src/Controller/GetItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class GetItemController
{
private const CHUNK = 1024;

/** @var Items */
private $items;

public function __construct(Items $items)
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/GetItemListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use function http_build_query;
use function is_string;
use const PHP_QUERY_RFC3986;

final class GetItemListController
Expand All @@ -17,7 +18,10 @@ final class GetItemListController
private const PER_PAGE = 'per-page';
private const DEFAULT_PER_PAGE = 20;

/** @var Items */
private $items;

/** @var string */
private $servicePrefix;

public function __construct(Items $items, string $servicePrefix)
Expand Down Expand Up @@ -48,7 +52,7 @@ public function __invoke(Request $request) : Response
['Content-Type' => 'application/xml; charset=utf-8']
);

if ($ids->getCursor()) {
if (is_string($ids->getCursor())) {
$query = clone $request->query;
$query->set(self::CURSOR, $ids->getCursor());
if (self::DEFAULT_PER_PAGE === $query->getInt(self::PER_PAGE, self::DEFAULT_PER_PAGE)) {
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/ContentApiConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

final class ContentApiConfiguration implements ConfigurationInterface
{
/** @var string */
private $rootName;

public function __construct(string $rootName)
Expand Down
1 change: 1 addition & 0 deletions src/EventListener/InvalidIdListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class InvalidIdListener
{
use TranslatingApiProblemListener;

/** @var TranslatorInterface */
private $translator;

public function __construct(TranslatorInterface $translator)
Expand Down
1 change: 1 addition & 0 deletions src/EventListener/InvalidVersionNumberListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class InvalidVersionNumberListener
{
use TranslatingApiProblemListener;

/** @var TranslatorInterface */
private $translator;

public function __construct(TranslatorInterface $translator)
Expand Down
1 change: 1 addition & 0 deletions src/EventListener/ItemNotFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class ItemNotFoundListener
{
use TranslatingApiProblemListener;

/** @var TranslatorInterface */
private $translator;

public function __construct(TranslatorInterface $translator)
Expand Down
6 changes: 5 additions & 1 deletion src/EventListener/SimplifiedApiProblemListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FluentDOM\DOM\Element;
use Libero\ApiProblemBundle\Event\CreateApiProblem;
use Throwable;
use function is_string;

trait SimplifiedApiProblemListener
{
Expand All @@ -22,7 +23,10 @@ final public function onCreateApiProblem(CreateApiProblem $event) : void

$problem->appendElement('status', (string) $this->status($event));
$problem->appendElement('title', $this->title($event));
if ($details = $this->details($event)) {

$details = $this->details($event);

if (is_string($details)) {
$problem->appendElement('details', $details);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/EventListener/TranslatingApiProblemListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ final protected function title(CreateApiProblem $event) : string

final protected function details(CreateApiProblem $event) : ?string
{
if (!$translation = $this->detailsTranslation($event)) {
$translation = $this->detailsTranslation($event);

if (!$translation instanceof TranslationRequest) {
return null;
}

Expand Down
8 changes: 8 additions & 0 deletions src/EventListener/TranslationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

final class TranslationRequest
{
/** @var string */
private $key;
/** @var array<string,mixed> */
private $parameters;

/**
* @param array<string,mixed> $parameters
*/
public function __construct(string $key, array $parameters = [])
{
$this->key = $key;
Expand All @@ -20,6 +25,9 @@ public function getKey() : string
return $this->key;
}

/**
* @return array<string,mixed>
*/
public function getParameters() : array
{
return $this->parameters;
Expand Down
1 change: 1 addition & 0 deletions src/EventListener/UnexpectedVersionNumberListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class UnexpectedVersionNumberListener
{
use TranslatingApiProblemListener;

/** @var TranslatorInterface */
private $translator;

public function __construct(TranslatorInterface $translator)
Expand Down
1 change: 1 addition & 0 deletions src/EventListener/VersionNotFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class VersionNotFoundListener
{
use TranslatingApiProblemListener;

/** @var TranslatorInterface */
private $translator;

public function __construct(TranslatorInterface $translator)
Expand Down
1 change: 1 addition & 0 deletions src/Exception/InvalidId.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class InvalidId extends UnexpectedValueException
{
/** @var string */
private $id;

public function __construct(string $id, ?Throwable $previous = null, int $code = 0)
Expand Down
1 change: 1 addition & 0 deletions src/Exception/InvalidVersionNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class InvalidVersionNumber extends UnexpectedValueException
{
/** @var string */
private $version;

public function __construct(string $version, ?Throwable $previous = null, int $code = 0)
Expand Down
1 change: 1 addition & 0 deletions src/Exception/ItemNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class ItemNotFound extends OutOfBoundsException
{
/** @var ItemId */
private $id;

public function __construct(ItemId $id, ?Throwable $previous = null, int $code = 0)
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/UnexpectedVersionNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@

class UnexpectedVersionNumber extends OutOfBoundsException
{
/** @var ItemVersionNumber */
private $expected;

/** @var ItemId */
private $id;

/** @var ItemVersionNumber */
private $version;

public function __construct(
Expand Down
3 changes: 3 additions & 0 deletions src/Exception/VersionNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

class VersionNotFound extends OutOfBoundsException
{
/** @var ItemId */
private $id;

/** @var ItemVersionNumber */
private $version;

public function __construct(ItemId $id, ItemVersionNumber $version, ?Throwable $previous = null, int $code = 0)
Expand Down
5 changes: 3 additions & 2 deletions src/Model/ItemId.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Libero\ContentApiBundle\Model;

use Libero\ContentApiBundle\Exception\InvalidId;
use function preg_match;
use function Libero\ContentApiBundle\matches;

final class ItemId
{
/** @var string */
private $id;

private function __construct(string $id)
Expand All @@ -23,7 +24,7 @@ public function __toString() : string

public static function fromString(string $id) : ItemId
{
if (!preg_match('/^([A-Za-z0-9-._~!$&\'()*+,;=:@]|%[A-F0-9]{2})+$/', $id)) {
if (!matches('/^([A-Za-z0-9-._~!$&\'()*+,;=:@]|%[A-F0-9]{2})+$/', $id)) {
throw new InvalidId($id);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Model/ItemListPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

final class ItemListPage implements Countable, Iterator
{
/** @var ?string */
private $cursor;
/** @var Iterator<ItemId> */
private $items;
/** @var int */
private $pointer;

/**
Expand Down
Loading