Skip to content

Commit

Permalink
Preparatory work to add a silent response (#32)
Browse files Browse the repository at this point in the history
* Preparatory work to add a silent response

Added a code enum to own our own HTTP code source so we can better work with Psalm annotations. We improved our code to the point that we are now at Psalm level 3, these code improvements have also had a positive effect on PHPStan as the last "phpstan-ignore" annotations could now be removed.

* Preparatory work to add a silent response

Fix code style

* Preparatory work to add a silent response

Update PHPUnit to version 10 and fix all deprecations of phpunit and php-cs-fixer

* Add a silent response

* Add a silent response

* Add infectionphp

now this package using infectionphp to check the qualitify of the unittests

* Add infectionphp

now this package using infectionphp to check the qualitify of the unittests

* Fix phpstan issues
  • Loading branch information
Dropelikeit committed Oct 25, 2023
1 parent 564b4b8 commit 96e94d7
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 82 deletions.
6 changes: 3 additions & 3 deletions .php-cs-fixer.dist.php
Expand Up @@ -12,12 +12,12 @@
'array_indentation' => true,
'array_syntax' => ['syntax'=>'short'],
'blank_line_before_statement' => true,
'braces' => false,
'single_space_around_construct' => false,
'cast_spaces' => true,
'concat_space' => ['spacing'=>'one'],
'declare_equal_normalize' => true,
'dir_constant' => true,
'function_typehint_space' => true,
'type_declaration_spaces' => true,
'include' => true,
'linebreak_after_opening_tag' => true,
'lowercase_cast' => true,
Expand Down Expand Up @@ -56,7 +56,7 @@
'return_type_declaration' => true,
'short_scalar_cast' => true,
'simplified_null_return' => true,
'single_blank_line_before_namespace' => true,
'blank_lines_before_namespace' => true,
'single_quote' => true,
'standardize_not_equals' => true,
'strict_comparison' => true,
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Expand Up @@ -4,6 +4,7 @@

Version 5 of this package no longer supports Laravel 8.
Since Laravel has not provided version 8 with updates since 24/01/2023.
Since Laravel 9+ requires PHP 8.1, this PHP version is also required in this library.
---

### Features
Expand Down
26 changes: 16 additions & 10 deletions composer.json
Expand Up @@ -15,7 +15,7 @@
"illuminate/http": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"illuminate/contracts": "^9.0|^10.0",
"jms/serializer": "^3.23"
"jms/serializer": "^3.27"
},
"autoload": {
"psr-4": {
Expand All @@ -37,23 +37,24 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"friendsofphp/php-cs-fixer": "^3.16",
"phpunit/phpunit": "^9.6",
"nunomaduro/larastan": "^2.5",
"orchestra/testbench": "^7.24",
"friendsofphp/php-cs-fixer": "^3.23",
"phpunit/phpunit": "^10.3",
"nunomaduro/larastan": "^2.6",
"orchestra/testbench": "^8.9",
"phpstan/phpstan-phpunit": "^1.3",
"php-parallel-lint/php-parallel-lint": "^1.3",
"symfony/cache": "^6.2",
"vimeo/psalm": "^5.9",
"symfony/cache": "^6.3",
"vimeo/psalm": "^5.15",
"psalm/plugin-laravel": "^2.8",
"psalm/plugin-phpunit": "^0.18.4"
"psalm/plugin-phpunit": "^0.18.4",
"infection/infection": "^0.27.6"
},
"scripts": {
"lint": "parallel-lint --exclude .git --exclude vendor .",
"cs-check": "php-cs-fixer -v --dry-run --using-cache=no fix",
"cs-fix": "php-cs-fixer --using-cache=no fix",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover build/logs/clover.xml",
"test": "export XDEBUG_MODE=coverage && phpunit",
"test-coverage": "export XDEBUG_MODE=coverage && phpunit --coverage-clover build/logs/clover.xml --coverage-html build/logs/clover.html",
"analyze": "phpstan analyze --no-progress --memory-limit=-1 --xdebug",
"psalm": "psalm --no-cache -c psalm.xml",
"check": [
Expand All @@ -64,5 +65,10 @@
"@lint",
"@psalm"
]
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
20 changes: 11 additions & 9 deletions phpunit.xml.dist
@@ -1,13 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutCoversAnnotation="true" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" cacheDirectory=".phpunit.cache" beStrictAboutCoverageMetadata="true">
<coverage>
<report>
<clover outputFile="build/output/tests/coverage.xml"/>
<text outputFile="php://stdout" showUncoveredFiles="false"/>
Expand All @@ -16,4 +9,13 @@
<testsuite name="Laravel-JMS-Serializer">
<directory suffix="Test.php">tests</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</source>
</phpunit>
3 changes: 2 additions & 1 deletion psalm.xml
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
errorLevel="3"
phpVersion="8.1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
73 changes: 44 additions & 29 deletions src/Config/Config.php
Expand Up @@ -7,8 +7,8 @@
use function array_keys;
use Dropelikeit\LaravelJmsSerializer\Contracts\Config as ResponseBuilderConfig;
use Dropelikeit\LaravelJmsSerializer\Contracts\CustomHandlerConfiguration;

use Dropelikeit\LaravelJmsSerializer\Exception\MissingRequiredItems;

use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType;
use function implode;
use function in_array;
Expand All @@ -23,30 +23,29 @@ final class Config implements ResponseBuilderConfig
/**
* @psalm-var non-empty-string
*/
private string $cacheDir;
private readonly string $cacheDir;

/**
* @var array<int, CustomHandlerConfiguration>
* @var array<int, CustomHandlerConfiguration|class-string>
*/
private array $customHandlers;
private readonly array $customHandlers;

/**
* @param array<int, CustomHandlerConfiguration> $customHandlers
* @psalm-param ResponseBuilderConfig::SERIALIZE_TYPE_* $serializeType
* @param array<int, CustomHandlerConfiguration|class-string> $customHandlers
*/
private function __construct(
string $cacheDir,
array $customHandlers,
private bool $shouldSerializeNull,
private readonly bool $shouldSerializeNull,
/**
* @var string 'json'|'xml'
* @psalm-var ResponseBuilderConfig::SERIALIZE_TYPE_*
*/
private string $serializeType,
private bool $debug,
private bool $addDefaultHandlers,
private readonly string $serializeType,
private readonly bool $debug,
private readonly bool $addDefaultHandlers,
string $cacheDir,
array $customHandlers,
) {
$cacheDir = sprintf('%s%s', $cacheDir, self::CACHE_DIR);
Assert::stringNotEmpty($cacheDir);
#Assert::stringNotEmpty($cacheDir);

$this->cacheDir = $cacheDir;
$this->customHandlers = $customHandlers;
Expand All @@ -60,32 +59,48 @@ private function __construct(
public static function fromConfig(array $config): self
{
$missing = array_diff([
'serialize_null',
'cache_dir',
'serialize_type',
'debug',
'add_default_handlers',
'custom_handlers',
self::KEY_SERIALIZE_NULL,
self::KEY_CACHE_DIR,
self::KEY_SERIALIZE_TYPE,
self::KEY_DEBUG,
self::KEY_ADD_DEFAULT_HANDLERS,
self::KEY_CUSTOM_HANDLERS,
], array_keys($config));

if (!empty($missing)) {
throw MissingRequiredItems::fromConfig(implode(',', $missing));
}

if (!in_array($config['serialize_type'], [
$serializeType = $config[self::KEY_SERIALIZE_TYPE];
if (!in_array($serializeType, [
self::SERIALIZE_TYPE_JSON,
self::SERIALIZE_TYPE_XML,
], true)) {
throw SerializeType::fromUnsupportedSerializeType($config['serialize_type']);
throw SerializeType::fromUnsupportedSerializeType($serializeType);
}

$serializeNull = $config[self::KEY_SERIALIZE_NULL];
Assert::boolean($serializeNull);

$debug = $config[self::KEY_DEBUG];
Assert::boolean($debug);

$addDefaultHandlers = $config[self::KEY_ADD_DEFAULT_HANDLERS];
Assert::boolean($addDefaultHandlers);

$cacheDir = $config[self::KEY_CACHE_DIR];
Assert::string($cacheDir);

$customHandlers = $config[self::KEY_CUSTOM_HANDLERS];
Assert::isArray($customHandlers);

return new self(
cacheDir: $config['cache_dir'],
customHandlers: (array) $config['custom_handlers'],
shouldSerializeNull: (bool) $config['serialize_null'],
serializeType: $config['serialize_type'],
debug: (bool) $config['debug'],
addDefaultHandlers: (bool) $config['add_default_handlers'],
shouldSerializeNull: $serializeNull,
serializeType: $serializeType,
debug: $debug,
addDefaultHandlers: $addDefaultHandlers,
cacheDir: $cacheDir,
customHandlers: $customHandlers,
);
}

Expand Down Expand Up @@ -121,7 +136,7 @@ public function shouldAddDefaultHeaders(): bool
}

/**
* @return array<int, CustomHandlerConfiguration>
* @return array<int, CustomHandlerConfiguration|class-string>
*/
public function getCustomHandlers(): array
{
Expand Down
9 changes: 8 additions & 1 deletion src/Contracts/Config.php
Expand Up @@ -12,6 +12,13 @@ interface Config
public const SERIALIZE_TYPE_XML = 'xml';
public const CACHE_DIR = '/serializer/';

public const KEY_SERIALIZE_NULL = 'serialize_null';
public const KEY_CACHE_DIR = 'cache_dir';
public const KEY_SERIALIZE_TYPE = 'serialize_type';
public const KEY_DEBUG = 'debug';
public const KEY_ADD_DEFAULT_HANDLERS = 'add_default_handlers';
public const KEY_CUSTOM_HANDLERS = 'custom_handlers';

public function getCacheDir(): string;

public function shouldSerializeNull(): bool;
Expand All @@ -23,7 +30,7 @@ public function debug(): bool;
public function shouldAddDefaultHeaders(): bool;

/**
* @psalm-return list<CustomHandlerConfiguration|class-string>
* @return array<int, CustomHandlerConfiguration|class-string>
*/
public function getCustomHandlers(): array;
}
9 changes: 7 additions & 2 deletions src/Contracts/ResponseBuilder.php
Expand Up @@ -4,6 +4,7 @@
namespace Dropelikeit\LaravelJmsSerializer\Contracts;

use Dropelikeit\LaravelJmsSerializer\Config\Config;
use Dropelikeit\LaravelJmsSerializer\Http\Code;
use JMS\Serializer\SerializationContext;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -13,8 +14,7 @@
interface ResponseBuilder
{
/**
* @phpstan-ignore-next-line
* @psalm-param Response::HTTP_* $code
* @psalm-param Code::HTTP_CODE_* $code
*/
public function withStatusCode(int $code): void;

Expand All @@ -34,4 +34,9 @@ public function create(object $jmsResponse): Response;
* @return Response
*/
public function createFromArray(array $jmsResponse): Response;

/**
* @description Create a response without a body
*/
public function createQuietResponse(): Response;
}
71 changes: 71 additions & 0 deletions src/Http/Code.php
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Http;

enum Code
{
public const HTTP_CODE_CONTINUE = 100;
public const HTTP_CODE_SWITCHING_PROTOCOLS = 101;
public const HTTP_CODE_PROCESSING = 102;
public const HTTP_CODE_EARLY_HINTS = 103;
public const HTTP_CODE_OK = 200;
public const HTTP_CODE_CREATED = 201;
public const HTTP_CODE_ACCEPTED = 202;
public const HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203;
public const HTTP_CODE_NO_CONTENT = 204;
public const HTTP_CODE_RESET_CONTENT = 205;
public const HTTP_CODE_PARTIAL_CONTENT = 206;
public const HTTP_CODE_MULTI_STATUS = 207;
public const HTTP_CODE_ALREADY_REPORTED = 208;
public const HTTP_CODE_IM_USED = 226;
public const HTTP_CODE_MULTIPLE_CHOICES = 300;
public const HTTP_CODE_MOVED_PERMANENTLY = 301;
public const HTTP_CODE_FOUND = 302;
public const HTTP_CODE_SEE_OTHER = 303;
public const HTTP_CODE_NOT_MODIFIED = 304;
public const HTTP_CODE_USE_PROXY = 305;
public const HTTP_CODE_RESERVED = 306;
public const HTTP_CODE_TEMPORARY_REDIRECT = 307;
public const HTTP_CODE_PERMANENTLY_REDIRECT = 308;
public const HTTP_CODE_BAD_REQUEST = 400;
public const HTTP_CODE_UNAUTHORIZED = 401;
public const HTTP_CODE_PAYMENT_REQUIRED = 402;
public const HTTP_CODE_FORBIDDEN = 403;
public const HTTP_CODE_NOT_FOUND = 404;
public const HTTP_CODE_METHOD_NOT_ALLOWED = 405;
public const HTTP_CODE_NOT_ACCEPTABLE = 406;
public const HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407;
public const HTTP_CODE_REQUEST_TIMEOUT = 408;
public const HTTP_CODE_CONFLICT = 409;
public const HTTP_CODE_GONE = 410;
public const HTTP_CODE_LENGTH_REQUIRED = 411;
public const HTTP_CODE_PRECONDITION_FAILED = 412;
public const HTTP_CODE_REQUEST_ENTITY_TOO_LARGE = 413;
public const HTTP_CODE_REQUEST_URI_TOO_LONG = 414;
public const HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415;
public const HTTP_CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
public const HTTP_CODE_EXPECTATION_FAILED = 417;
public const HTTP_CODE_I_AM_A_TEAPOT = 418;
public const HTTP_CODE_MISDIRECTED_REQUEST = 421;
public const HTTP_CODE_UNPROCESSABLE_ENTITY = 422;
public const HTTP_CODE_LOCKED = 423;
public const HTTP_CODE_FAILED_DEPENDENCY = 424;
public const HTTP_CODE_TOO_EARLY = 425;
public const HTTP_CODE_UPGRADE_REQUIRED = 426;
public const HTTP_CODE_PRECONDITION_REQUIRED = 428;
public const HTTP_CODE_TOO_MANY_REQUESTS = 429;
public const HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
public const HTTP_CODE_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
public const HTTP_CODE_INTERNAL_SERVER_ERROR = 500;
public const HTTP_CODE_NOT_IMPLEMENTED = 501;
public const HTTP_CODE_BAD_GATEWAY = 502;
public const HTTP_CODE_SERVICE_UNAVAILABLE = 503;
public const HTTP_CODE_GATEWAY_TIMEOUT = 504;
public const HTTP_CODE_VERSION_NOT_SUPPORTED = 505;
public const HTTP_CODE_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;
public const HTTP_CODE_INSUFFICIENT_STORAGE = 507;
public const HTTP_CODE_LOOP_DETECTED = 508;
public const HTTP_CODE_NOT_EXTENDED = 510;
public const HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511;
}

0 comments on commit 96e94d7

Please sign in to comment.