Skip to content

Commit

Permalink
Merge pull request #5 from nynka/feature/php-8-support
Browse files Browse the repository at this point in the history
PHP 8 Support
  • Loading branch information
Ocramius committed Dec 15, 2020
2 parents 3ece019 + 0322ee9 commit 5ef82fb
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 194 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.phpcs-cache
/.phpunit.result.cache
/clover.xml
/composer.lock
/coveralls-upload.json
Expand Down
16 changes: 9 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
- php: 7.3
env:
- DEPS=lowest
- php: 7.1
- php: 7.3
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: 7.2
- php: 7.4
env:
- DEPS=lowest
- php: 7.2
- php: 7.4
env:
- DEPS=latest
- php: 7.3
- php: 8.0
env:
- DEPS=lowest
- php: 7.3
- COMPOSER_ARGS="--no-interaction --ignore-platform-reqs"
- php: 8.0
env:
- DEPS=latest
- COMPOSER_ARGS="--no-interaction --ignore-platform-reqs"

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand All @@ -44,7 +46,7 @@ install:
- stty cols 120 && composer show

script:
- if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else composer test ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then XDEBUG_MODE=coverage composer test-coverage ; else composer test ; fi
- if [[ $CS_CHECK == 'true' ]]; then composer cs-check ; fi

after_script:
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@
}
},
"require": {
"php": "^7.1",
"php": "^7.3 || ~8.0.0",
"laminas/laminas-zendframework-bridge": "^1.0",
"mezzio/mezzio-session": "^1.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"phpunit/phpunit": "^7.0.2"
},
"conflict": {
"phpspec/prophecy": "<1.7.2"
"laminas/laminas-coding-standard": "~2.1.0",
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
</ruleset>
21 changes: 10 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="mezzio-flash">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="mezzio-flash">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 3 additions & 3 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

class ConfigProvider
{
public function __invoke() : array
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

public function getDependencies() : array
public function getDependencies(): array
{
return [
// Legacy Zend Framework aliases
'aliases' => [
'aliases' => [
\Zend\Expressive\Flash\FlashMessageMiddleware::class => FlashMessageMiddleware::class,
],
'invokables' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class InvalidFlashMessagesImplementationException extends InvalidArgumentException implements ExceptionInterface
{
public static function forClass(string $class) : self
public static function forClass(string $class): self
{
return new self(sprintf(
'Cannot use "%s" within %s; does not implement %s',
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidHopsValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class InvalidHopsValueException extends InvalidArgumentException implements ExceptionInterface
{
public static function valueTooLow(string $key, int $hops) : self
public static function valueTooLow(string $key, int $hops): self
{
return new self(sprintf(
'Hops value specified for flash message "%s" was too low; must be greater than 0, received %d',
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/MissingSessionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class MissingSessionException extends RuntimeException implements ExceptionInterface
{
public static function forMiddleware(MiddlewareInterface $middleware)
public static function forMiddleware(MiddlewareInterface $middleware): MissingSessionException
{
return new self(sprintf(
'Unable to create flash messages in %s; missing session attribute',
Expand Down
21 changes: 8 additions & 13 deletions src/FlashMessageMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,33 @@ class FlashMessageMiddleware implements MiddlewareInterface
{
public const FLASH_ATTRIBUTE = 'flash';

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

/**
* @var callable
*/
/** @var callable */
private $flashMessageFactory;

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

public function __construct(
string $flashMessagesClass = FlashMessages::class,
string $sessionKey = FlashMessagesInterface::FLASH_NEXT,
string $attributeKey = self::FLASH_ATTRIBUTE
) {
if (! class_exists($flashMessagesClass)
if (
! class_exists($flashMessagesClass)
|| ! in_array(FlashMessagesInterface::class, class_implements($flashMessagesClass), true)
) {
throw Exception\InvalidFlashMessagesImplementationException::forClass($flashMessagesClass);
}

$this->flashMessageFactory = [$flashMessagesClass, 'createFromSession'];
$this->sessionKey = $sessionKey;
$this->attributeKey = $attributeKey;
$this->sessionKey = $sessionKey;
$this->attributeKey = $attributeKey;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE, false);
if (! $session instanceof SessionInterface) {
Expand Down
32 changes: 13 additions & 19 deletions src/FlashMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,18 @@
*/
class FlashMessages implements FlashMessagesInterface
{
/**
* @var array
*/
/** @var array */
private $currentMessages = [];

/**
* @var SessionInterface
*/
/** @var SessionInterface */
private $session;

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

private function __construct(SessionInterface $session, string $sessionKey)
{
$this->session = $session;
$this->session = $session;
$this->sessionKey = $sessionKey;
$this->prepareMessages($session, $sessionKey);
}
Expand All @@ -63,7 +57,7 @@ private function __construct(SessionInterface $session, string $sessionKey)
public static function createFromSession(
SessionInterface $session,
string $sessionKey = FlashMessagesInterface::FLASH_NEXT
) : FlashMessagesInterface {
): FlashMessagesInterface {
return new self($session, $sessionKey);
}

Expand All @@ -77,13 +71,13 @@ public static function createFromSession(
* @param mixed $value
* @throws Exception\InvalidHopsValueException
*/
public function flash(string $key, $value, int $hops = 1) : void
public function flash(string $key, $value, int $hops = 1): void
{
if ($hops < 1) {
throw Exception\InvalidHopsValueException::valueTooLow($key, $hops);
}

$messages = $this->session->get($this->sessionKey, []);
$messages = $this->session->get($this->sessionKey, []);
$messages[$key] = [
'value' => $value,
'hops' => $hops,
Expand All @@ -100,7 +94,7 @@ public function flash(string $key, $value, int $hops = 1) : void
*
* @param mixed $value
*/
public function flashNow(string $key, $value, int $hops = 1) : void
public function flashNow(string $key, $value, int $hops = 1): void
{
$this->currentMessages[$key] = $value;
$this->flash($key, $value, $hops);
Expand Down Expand Up @@ -132,7 +126,7 @@ public function getFlash(string $key, $default = null)
*
* @return array
*/
public function getFlashes() : array
public function getFlashes(): array
{
return $this->currentMessages;
}
Expand All @@ -142,15 +136,15 @@ public function getFlashes() : array
*
* Affects the next and subsequent requests.
*/
public function clearFlash() : void
public function clearFlash(): void
{
$this->session->unset($this->sessionKey);
}

/**
* Prolongs any current flash messages for one more hop.
*/
public function prolongFlash() : void
public function prolongFlash(): void
{
$messages = $this->session->get($this->sessionKey, []);
foreach ($this->currentMessages as $key => $value) {
Expand All @@ -162,7 +156,7 @@ public function prolongFlash() : void
}
}

public function prepareMessages(SessionInterface $session, string $sessionKey) : void
public function prepareMessages(SessionInterface $session, string $sessionKey): void
{
if (! $session->has($sessionKey)) {
return;
Expand All @@ -180,7 +174,7 @@ public function prepareMessages(SessionInterface $session, string $sessionKey) :
continue;
}

$data['hops'] -= 1;
$data['hops'] -= 1;
$sessionMessages[$key] = $data;
}

Expand Down
12 changes: 6 additions & 6 deletions src/FlashMessagesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface FlashMessagesInterface
public static function createFromSession(
SessionInterface $session,
string $sessionKey = self::FLASH_NEXT
) : FlashMessagesInterface;
): FlashMessagesInterface;

/**
* Set a flash value with the given key.
Expand All @@ -39,7 +39,7 @@ public static function createFromSession(
*
* @param mixed $value
*/
public function flash(string $key, $value, int $hops = 1) : void;
public function flash(string $key, $value, int $hops = 1): void;

/**
* Set a flash value with the given key, but allow access during this request.
Expand All @@ -50,7 +50,7 @@ public function flash(string $key, $value, int $hops = 1) : void;
*
* @param mixed $value
*/
public function flashNow(string $key, $value, int $hops = 1) : void;
public function flashNow(string $key, $value, int $hops = 1): void;

/**
* Retrieve a flash value.
Expand All @@ -75,17 +75,17 @@ public function getFlash(string $key, $default = null);
*
* @return array
*/
public function getFlashes() : array;
public function getFlashes(): array;

/**
* Clear all flash values.
*
* Affects the next and subsequent requests.
*/
public function clearFlash() : void;
public function clearFlash(): void;

/**
* Prolongs any current flash messages for one more hop.
*/
public function prolongFlash() : void;
public function prolongFlash(): void;
}
Loading

0 comments on commit 5ef82fb

Please sign in to comment.