Skip to content

Add support for php8.4 and other improvements #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
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
15 changes: 8 additions & 7 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
* text=auto eol=lf

/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml export-ignore
phpcs.xml export-ignore
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php-cs-fixer.php export-ignore
phpcs.xml.dist export-ignore
phpunit.xml.dist export-ignore
.phpstan.neon export-ignore
49 changes: 23 additions & 26 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: "testing"

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
qa:
Expand All @@ -13,23 +13,23 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest
run: composer install --prefer-dist --no-progress

- name: Coding Standard
run: composer run-script cs
Expand All @@ -40,37 +40,34 @@ jobs:

strategy:
matrix:
php:
- 7.2
- 7.3
- 7.4
composer-args: [ "" ]
include:
- php: 8.0
composer-args: --ignore-platform-reqs
fail-fast: false
php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Cache PHP dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest ${{ matrix.composer-args }}
run: composer install --prefer-dist --no-progress

- name: Tests
run: composer test

- name: Tests coverage
run: composer coverage
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ build
composer.lock
phpunit.xml
.php_cs.cache
.idea
kit
*.cache
10 changes: 10 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return My\PhpCsFixerConfig::create()
->setFinder(
PhpCsFixer\Finder::create()
->files()
->name('*.php')
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
);
10 changes: 10 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
reportUnmatchedIgnoredErrors: false
level: 8
paths:
- src
- tests
ignoreErrors:
-
identifier: 'isset.offset'
reportUnmatched: false
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.1.0] - 2025-03-21
### Added
- Support for PHP 8.4

## [1.0.0] - 2018-10-04

- Initial Release

[1.1.0]: https://github.com/middlewares/image-manipulation/compare/v1.0.0...v1.1.0
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018
Copyright (c) 2018-2025

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 12 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
},
"require": {
"php": "^7.2 || ^8.0",
"middlewares/utils": "^2.1",
"psr/http-server-middleware": "^1.0",
"wyrihaximus/json-throwable": "^2.0",
"thecodingmachine/safe": "^0.1.5"
"middlewares/utils": "^2 || ^3 || ^4",
"psr/http-server-middleware": "^1",
"wyrihaximus/json-throwable": "^2 || ^3 || ^4",
"thecodingmachine/safe": "^1 || ^2 || ^3"
},
"require-dev": {
"phpunit/phpunit": "^8|^9",
"zendframework/zend-diactoros": "^1.3",
"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^0.12"
"phpunit/phpunit": "^8 || ^9",
"laminas/laminas-diactoros": "^2 || ^3",
"friendsofphp/php-cs-fixer": "^3",
"squizlabs/php_codesniffer": "^3",
"phpstan/phpstan": "^1 || ^2",
"oscarotero/php-cs-fixer-config": "^2",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand All @@ -49,4 +51,4 @@
"coverage": "phpunit --coverage-text",
"coverage-html": "phpunit --coverage-html=coverage"
}
}
}
16 changes: 16 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset name="Middlewares coding standard">
<description>Middlewares coding standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="report" value="full"/>
<arg name="colors"/>

<!-- coding standard -->
<rule ref="PSR2"/>

<!-- Paths to check -->
<file>src</file>
<file>tests</file>
</ruleset>
24 changes: 18 additions & 6 deletions src/JsonExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace Middlewares;

Expand All @@ -10,9 +10,8 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;

use function Safe\json_encode;
use Throwable;
use function WyriHaximus\throwable_encode;

class JsonExceptionHandler implements MiddlewareInterface
Expand All @@ -33,8 +32,8 @@ class JsonExceptionHandler implements MiddlewareInterface
private $jsonOptions = 0;

public function __construct(
ResponseFactoryInterface $responseFactory = null,
StreamFactoryInterface $streamFactory = null
?ResponseFactoryInterface $responseFactory = null,
?StreamFactoryInterface $streamFactory = null
) {
$this->responseFactory = $responseFactory ?? Factory::getResponseFactory();
$this->streamFactory = $streamFactory ?? Factory::getStreamFactory();
Expand Down Expand Up @@ -87,7 +86,20 @@ protected function exceptionResponse(Throwable $e): ResponseInterface
$exceptionJson = throwable_encode($e);

if ($this->includeTrace === false) {
$exceptionJson['trace'] = [];
// version 2 (php 7.2 and 7.3)
/* @phpstan-ignore isset.offset */
if (isset($exceptionJson['trace'])) {
$exceptionJson['trace'] = [];
}

// version 3 and 4 (7.4, >= 8.0)
/* @phpstan-ignore isset.offset */
if (isset($exceptionJson['originalTrace'])) {
$exceptionJson['originalTrace'] = [];
$exceptionJson['additionalProperties'] = [
'trace' => serialize([]),
];
}
}

$exceptionJson = json_encode($exceptionJson, $this->jsonOptions);
Expand Down
11 changes: 5 additions & 6 deletions tests/JsonExceptionHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace Middlewares\Tests;

Expand All @@ -8,12 +8,11 @@
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory;
use PHPUnit\Framework\TestCase;

use function WyriHaximus\throwable_json_decode;

class JsonExceptionHandlerTest extends TestCase
{
public function testMiddleware()
public function testMiddleware(): void
{
$middleware = new JsonExceptionHandler();

Expand All @@ -35,7 +34,7 @@ public function testMiddleware()
$this->assertSame('Something went wrong', $e->getMessage());
}

public function testContentType()
public function testContentType(): void
{
$middleware = new JsonExceptionHandler();
$middleware->contentType($type = 'application/json;charset=utf-8');
Expand All @@ -54,7 +53,7 @@ public function testContentType()
$this->assertSame($type, $response->getHeaderLine('Content-Type'));
}

public function testDisableTrace()
public function testDisableTrace(): void
{
$middleware = new JsonExceptionHandler();
$middleware->includeTrace(false);
Expand All @@ -77,7 +76,7 @@ public function testDisableTrace()
$this->assertEmpty($e->getTrace());
}

public function testJsonOptions()
public function testJsonOptions(): void
{
$middleware = new JsonExceptionHandler();
$middleware->jsonOptions(JSON_PRETTY_PRINT);
Expand Down