Skip to content

Commit

Permalink
Merge pull request #236 from t0mmy742/bugfix/deprecated_warning_php81
Browse files Browse the repository at this point in the history
Fix deprecation warning PHP 8.1 (+ small things)
  • Loading branch information
l0gicgate authored Oct 21, 2021
2 parents a5edc13 + e93f5dc commit 5f5a000
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 113 deletions.
8 changes: 5 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

# Exclude unused files
# see: https://redd.it/2jzp6k
/tests export-ignore
/.coveralls.yml export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/phpcs.xml export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"php": "^7.3 || ^8.0",
"psr/http-message": "^1.0",
"slim/slim": "^4.9",
"twig/twig": "^3.3"
"twig/twig": "^3.3",
"symfony/polyfill-php81": "^1.23"
},
"require-dev": {
"phpunit/phpunit": "^9.3.8",
"phpunit/phpunit": "^9.5",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^0.12.99",
"psr/http-factory": "^1.0",
Expand All @@ -48,6 +49,6 @@
],
"phpunit": "phpunit",
"phpcs": "phpcs",
"phpstan": "phpstan analyse src --memory-limit=-1"
"phpstan": "phpstan"
}
}
2 changes: 1 addition & 1 deletion phpcs.xml → phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<arg name="colors"/>

<!-- inherit rules from: -->
<rule ref="PSR2"/>
<rule ref="PSR12"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>

<!-- Paths to check -->
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
parameters:
level: max
inferPrivatePropertyTypeFromConstructor: true
checkGenericClassInNonGenericObjectType: false
paths:
- src
18 changes: 5 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
<?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"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Twig-View Test Suite">
<directory>./tests/</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
<logging />

<coverage processUncoveredFiles="true">
<include>
<directory>./src/</directory>
<directory>src</directory>
</include>
<report>
<html outputDirectory="./coverage" lowUpperBound="20" highLowerBound="50"/>
<html outputDirectory="coverage" lowUpperBound="20" highLowerBound="50"/>
</report>
</coverage>
</phpunit>
15 changes: 13 additions & 2 deletions src/Twig.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand All @@ -13,6 +14,7 @@
use ArrayIterator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReturnTypeWillChange;
use RuntimeException;
use Throwable;
use Twig\Environment;
Expand All @@ -24,13 +26,21 @@
use Twig\Loader\LoaderInterface;
use Twig\RuntimeLoader\RuntimeLoaderInterface;

use function array_key_exists;
use function array_merge;
use function count;
use function is_array;
use function is_string;

/**
* Twig View
*
* This class is a Slim Framework view helper built on top of the Twig templating component.
* Twig is a PHP component created by Fabien Potencier.
*
* @link https://twig.symfony.com/
*
* @implements ArrayAccess<string, mixed>
*/
class Twig implements ArrayAccess
{
Expand Down Expand Up @@ -64,9 +74,9 @@ class Twig implements ArrayAccess
public static function fromRequest(ServerRequestInterface $request, string $attributeName = 'view'): self
{
$twig = $request->getAttribute($attributeName);
if ($twig === null || !($twig instanceof self)) {
if (!($twig instanceof self)) {
throw new RuntimeException(
'Twig could not be found in the server request attributes using the key "'. $attributeName .'".'
'Twig could not be found in the server request attributes using the key "' . $attributeName . '".'
);
}

Expand Down Expand Up @@ -245,6 +255,7 @@ public function offsetExists($key): bool
*
* @return mixed The key's value, or the default value
*/
#[ReturnTypeWillChange]
public function offsetGet($key)
{
if (!$this->offsetExists($key)) {
Expand Down
1 change: 1 addition & 0 deletions src/TwigExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand Down
2 changes: 1 addition & 1 deletion src/TwigMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand All @@ -9,7 +10,6 @@

namespace Slim\Views;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down
7 changes: 4 additions & 3 deletions src/TwigRuntimeExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand Down Expand Up @@ -77,7 +78,7 @@ public function fullUrlFor(string $routeName, array $data = [], array $queryPara
*/
public function isCurrentUrl(string $routeName, array $data = []): bool
{
$currentUrl = $this->basePath.$this->uri->getPath();
$currentUrl = $this->basePath . $this->uri->getPath();
$result = $this->routeParser->urlFor($routeName, $data);

return $result === $currentUrl;
Expand All @@ -92,11 +93,11 @@ public function isCurrentUrl(string $routeName, array $data = []): bool
*/
public function getCurrentUrl(bool $withQueryString = false): string
{
$currentUrl = $this->basePath.$this->uri->getPath();
$currentUrl = $this->basePath . $this->uri->getPath();
$query = $this->uri->getQuery();

if ($withQueryString && !empty($query)) {
$currentUrl .= '?'.$query;
$currentUrl .= '?' . $query;
}

return $currentUrl;
Expand Down
1 change: 1 addition & 0 deletions src/TwigRuntimeLoader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand Down
51 changes: 0 additions & 51 deletions tests/Mocks/MockContainerWithArrayAccess.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand All @@ -13,6 +14,8 @@
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
use ReflectionProperty;

use function get_class;

abstract class TestCase extends PhpUnitTestCase
{
use ProphecyTrait;
Expand Down
3 changes: 3 additions & 0 deletions tests/TwigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand All @@ -11,6 +12,8 @@

use Slim\Views\TwigExtension;

use function count;

class TwigExtensionTest extends TestCase
{
public function testGetName()
Expand Down
22 changes: 6 additions & 16 deletions tests/TwigMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (http://slimframework.com)
*
Expand All @@ -24,6 +25,7 @@
use Slim\Views\TwigMiddleware;
use Slim\Views\TwigRuntimeExtension;
use Slim\Views\TwigRuntimeLoader;
use Twig\RuntimeLoader\RuntimeLoaderInterface;

class TwigMiddlewareTest extends TestCase
{
Expand All @@ -33,17 +35,16 @@ class TwigMiddlewareTest extends TestCase
* @param ObjectProphecy $uriProphecy
* @param string $basePath
*
* @return ObjectProphecy
* @return ObjectProphecy&Twig
*/
private function createTwigProphecy(ObjectProphecy $uriProphecy, string $basePath): ObjectProphecy
private function createTwigProphecy(ObjectProphecy $uriProphecy, string $basePath)
{
$self = $this;

$twigProphecy = $this->prophesize(Twig::class);

/** @noinspection PhpUndefinedMethodInspection */
$twigProphecy->
addRuntimeLoader(Argument::type('object'))
$twigProphecy
->addRuntimeLoader(Argument::type(RuntimeLoaderInterface::class))
->will(function ($args) use ($self, $uriProphecy, $basePath) {
/** @var TwigRuntimeLoader $runtimeLoader */
$runtimeLoader = $args[0];
Expand Down Expand Up @@ -167,7 +168,6 @@ public function testProcess()
$twigProphecy = $this->createTwigProphecy($uriProphecy, $basePath);
$routeParserProphecy = $this->prophesize(RouteParserInterface::class);

/** @noinspection PhpParamsInspection */
$twigMiddleware = new TwigMiddleware(
$twigProphecy->reveal(),
$routeParserProphecy->reveal(),
Expand All @@ -177,20 +177,17 @@ public function testProcess()
$responseProphecy = $this->prophesize(ResponseInterface::class);

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
/** @noinspection PhpUndefinedMethodInspection */
$requestProphecy
->getUri()
->willReturn($uriProphecy->reveal())
->shouldBeCalledOnce();

$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
/** @noinspection PhpUndefinedMethodInspection */
$requestHandlerProphecy
->handle($requestProphecy->reveal())
->shouldBeCalledOnce()
->willReturn($responseProphecy->reveal());

/** @noinspection PhpParamsInspection */
$twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
}

Expand All @@ -211,20 +208,16 @@ public function testProcessWithRequestAttribute()

// Prophesize the server request.
$requestProphecy = $this->prophesize(ServerRequestInterface::class);
/** @noinspection PhpUndefinedMethodInspection */
$requestProphecy->withAttribute('view', Argument::type(Twig::class))
->shouldBeCalledOnce()
->will(function ($args) use ($requestProphecy2): ServerRequestInterface {
/** @noinspection PhpUndefinedMethodInspection */
$requestProphecy2->getAttribute('view')
->shouldBeCalledOnce()
->willReturn($args[1]);

/** @noinspection PhpIncompatibleReturnTypeInspection */
return $requestProphecy2->reveal();
});

/** @noinspection PhpUndefinedMethodInspection */
$requestProphecy
->getUri()
->willReturn($uriProphecy->reveal())
Expand All @@ -233,7 +226,6 @@ public function testProcessWithRequestAttribute()
// Prophesize the request handler.
$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
$that = $this;
/** @noinspection PhpUndefinedMethodInspection */
$requestHandlerProphecy
->handle($requestProphecy2->reveal())
->shouldBeCalledOnce()
Expand All @@ -242,11 +234,9 @@ public function testProcessWithRequestAttribute()
$serverRequest = $args[0];
$that->assertSame($twig, $serverRequest->getAttribute('view'));

/** @noinspection PhpIncompatibleReturnTypeInspection */
return $responseProphecy->reveal();
});

/** @noinspection PhpParamsInspection */
$twigMiddleware->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
}
}
Loading

0 comments on commit 5f5a000

Please sign in to comment.