Skip to content

Commit

Permalink
Implemented PHP 8.2 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbankowski committed Jun 19, 2023
1 parent bf1c22a commit 3073dc5
Show file tree
Hide file tree
Showing 21 changed files with 396 additions and 527 deletions.
2 changes: 1 addition & 1 deletion Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.0
FROM php:8.2.7

WORKDIR /var/www

Expand Down
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"config": {
"platform": {
"php": "7.4.11"
}
"php": "8.2.7"
},
"allow-plugins": true
},
"name": "letsdrink/ouzo",
"description": "Ouzo PHP MVC framework",
Expand Down Expand Up @@ -41,16 +42,16 @@
"require": {
"ext-json": "*",
"ext-fileinfo": "*",
"symfony/console": "^5.1",
"symfony/event-dispatcher": "^5.1",
"symfony/console": "^5.4",
"symfony/event-dispatcher": "^5.4",
"filp/whoops": "2.8.*",
"psr/log": "^1.0",
"ocramius/proxy-manager": "^2.13",
"doctrine/annotations": "^1.10"
"psr/log": "^1.1",
"friendsofphp/proxy-manager-lts": "1.0.16",
"doctrine/annotations": "^1.14"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"php-coveralls/php-coveralls": "^2.4"
"phpunit/phpunit": "^9.6",
"php-coveralls/php-coveralls": "^2.5"
},
"replace": {
"letsdrink/ouzo-goodies": "self.version",
Expand Down
787 changes: 315 additions & 472 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Ouzo/Core/Csrf/CsrfProtector.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function getCsrfToken(): string
private static function generateCsrfToken(): string
{
$length = 32;
$bytes = openssl_random_pseudo_bytes(($length + 1) / 2);
$bytes = openssl_random_pseudo_bytes(round(($length + 1) / 2));
$hex = bin2hex($bytes);
return substr(base64_encode($hex), 0, $length);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ouzo/Core/Db/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function trace(string $query, mixed $params, Closure $function): m

public static function getTotalTime(): int
{
return array_reduce(self::$queries, fn($sum, $value) => $sum + $value['time']);
return round(array_reduce(self::$queries, fn($sum, $value) => $sum + $value['time']));
}

public static function reset(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Ouzo/Core/Helper/HtmlElementRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ouzo\Helper;

use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Strings;

class HtmlElementRenderer
{
Expand Down Expand Up @@ -166,7 +167,7 @@ private function attributesToHtml(array $attributes): string
$attributes = $this->sort($attributes);

$attrs = Arrays::mapEntries($attributes, function ($key, $value) {
$escaped = htmlspecialchars($value);
$escaped = htmlspecialchars($value ?? Strings::EMPTY_STRING);
return "$key=\"$escaped\"";
});
return implode(' ', $attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/Ouzo/Core/Middleware/Interceptor/DefaultRequestId.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function handleRequestContext(RequestContext $requestContext, Chain $nex
$id = uniqid();
if (FrontController::$requestId === null) {
FrontController::$requestId = $id;
$requestContext->id = $id;
$requestContext->setId($id);
}

return $next->proceed($requestContext);
Expand Down
14 changes: 5 additions & 9 deletions src/Ouzo/Core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
use Ouzo\Utilities\Strings;
use PDO;
use ReflectionClass;
use Serializable;

class Model extends Validatable implements Serializable, JsonSerializable
class Model extends Validatable implements JsonSerializable
{
private ModelDefinition $modelDefinition;
private array $attributes;
Expand Down Expand Up @@ -508,17 +507,14 @@ private function getAttributesForUpdate(): array
return array_intersect_key($attributes, array_flip($this->modifiedFields));
}

public function serialize(): string
public function __serialize(): array
{
return serialize($this->attributes);
return $this->attributes;
}

public function unserialize($serialized): void
public function __unserialize(array $serialized): void
{
$result = unserialize($serialized);
foreach ($result as $key => $value) {
$this->$key = $value;
}
$this->attributes = $serialized;
}

public function jsonSerialize(): string
Expand Down
12 changes: 12 additions & 0 deletions src/Ouzo/Core/Request/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class RequestContext
{
private string $id;

public function __construct(
private string $controller,
private string $action,
Expand All @@ -32,4 +34,14 @@ public function getCurrentAction(): string
{
return $this->action;
}

public function getId(): string
{
return $this->id;
}

public function setId(string $id): void
{
$this->id = $id;
}
}
2 changes: 1 addition & 1 deletion src/Ouzo/Core/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function parsePath(string $path = null, int $limit = null): array
if ($path != null) {
$prefix = Config::getValue('global', 'prefix_system');
$pathWithoutPrefix = urldecode(str_replace($prefix, '', $path));
return preg_split('#/|\?#', $pathWithoutPrefix, $limit, PREG_SPLIT_NO_EMPTY);
return preg_split('#/|\?#', $pathWithoutPrefix, $limit ?: -1, PREG_SPLIT_NO_EMPTY);
}
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ouzo/Core/Uri/Es6GeneratorTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function replace(string $template, array $replacements): string
{
$result = $template;
foreach ($replacements as $key => $value) {
$result = str_replace("${key}_REPLACEMENT", $value, $result);
$result = str_replace("{$key}_REPLACEMENT", $value, $result);
}
return $result;
}
Expand Down
13 changes: 8 additions & 5 deletions src/Ouzo/Core/View/PhtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ouzo\View;

use Exception;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Files;
use Ouzo\Utilities\Strings;

Expand All @@ -27,7 +28,6 @@ public function __construct(

public function render(): string
{
$this->saveAttributes();
ob_start();
try {
$this->loadViewHelper();
Expand All @@ -53,11 +53,14 @@ private function loadView(): void
require($this->viewPath);
}

private function saveAttributes(): void
public function __get(string $name)
{
foreach ($this->attributes as $name => $value) {
$this->$name = $value;
}
return Arrays::getValue($this->attributes, $name);
}

public function __set(string $name, $value): void
{
$this->attributes[$name] = $value;
}

public function getViewPath(): string
Expand Down
11 changes: 4 additions & 7 deletions src/Ouzo/Goodies/Utilities/DynamicProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ private static function getProxyClassDefinition(string $name, string $className)
private static function getClassMethods(ReflectionClass $reflectionClass): array
{
$reflectionMethods = $reflectionClass->getMethods();
return Arrays::filter($reflectionMethods, function (ReflectionMethod $reflectionMethod) {
return !$reflectionMethod->isConstructor();
});
return Arrays::filter($reflectionMethods, fn(ReflectionMethod $reflectionMethod) => !$reflectionMethod->isConstructor());
}

private static function generateMethod(ReflectionMethod $reflectionMethod): string
Expand All @@ -69,7 +67,7 @@ private static function generateMethod(ReflectionMethod $reflectionMethod): stri
$invoke = 'call_user_func_array(array($this->methodHandler, __FUNCTION__), func_get_args());';

if (is_null($returnTypeInfo)) {
$methodSignature = "{$modifier} function {$methodName}({$parameters})";
$methodSignature = "#[\ReturnTypeWillChange]\n{$modifier} function {$methodName}({$parameters})";
$methodBody = "return {$invoke}";
} elseif ($returnTypeInfo['type'] === 'void') {
$methodSignature = "{$modifier} function {$methodName}({$parameters}): void";
Expand Down Expand Up @@ -102,7 +100,7 @@ private static function generateParameters(ReflectionMethod $reflectionMethod):
$parameter = '';
if ($reflectionParameter->hasType() && $reflectionParameter->getType() instanceof ReflectionNamedType) {
$typeName = $reflectionParameter->getType()->getName();
$nullable = $typeName != 'mixed' && $reflectionParameter->allowsNull() ? '?' : '';
$nullable = $typeName != 'mixed' && $reflectionParameter->allowsNull() ? '?' : Strings::EMPTY_STRING;
$parameter .= "{$nullable}{$typeName} ";
}
if ($reflectionParameter->isVariadic()) {
Expand All @@ -127,7 +125,6 @@ private static function getReturnTypeInfo(ReflectionMethod $reflectionMethod): ?
$methodReturnType = $reflectionMethod->getReturnType();

if ($methodReturnType instanceof ReflectionUnionType) {
/** @var ReflectionUnionType $methodReturnType */
$type = implode('|', Arrays::map($methodReturnType->getTypes(), fn($type) => $type->getName()));
return [
'type' => $type,
Expand Down Expand Up @@ -157,6 +154,6 @@ private static function generateRelation(string $relation, array $classes): stri
if ($classes) {
return $relation . ' ' . implode(', ', $classes);
}
return '';
return Strings::EMPTY_STRING;
}
}
2 changes: 1 addition & 1 deletion src/Ouzo/Goodies/Utilities/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function constant(mixed $value): Closure

public static function random(int $min = 0, ?int $max = null): Closure
{
return fn() => mt_rand($min, $max);
return fn() => mt_rand($min, $max ?: mt_getrandmax());
}

public static function throwException(Exception $exception): Closure
Expand Down
2 changes: 1 addition & 1 deletion src/Ouzo/Goodies/Utilities/Iterator/FilteringIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(Iterator $iterator, callable $predicate)
$this->predicate = Closure::fromCallable($predicate);
}

public function accept(): mixed
public function accept(): bool
{
$predicate = $this->predicate;
return $predicate($this->current());
Expand Down
19 changes: 14 additions & 5 deletions src/Ouzo/Goodies/Utilities/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function removeSuffix(?string $string, ?string $suffix): ?string
*/
public static function startsWith(?string $string, ?string $prefix): bool
{
if (is_null($prefix)) {
if (is_null($string) || is_null($prefix)) {
return false;
}
return str_starts_with($string, $prefix);
Expand All @@ -146,7 +146,7 @@ public static function startsWith(?string $string, ?string $prefix): bool
*/
public static function endsWith(?string $string, ?string $suffix): bool
{
if (is_null($suffix)) {
if (is_null($string) || is_null($suffix)) {
return false;
}
return str_ends_with($string, $suffix);
Expand All @@ -164,6 +164,9 @@ public static function endsWith(?string $string, ?string $suffix): bool
*/
public static function equalsIgnoreCase(?string $string1, ?string $string2): bool
{
if (is_null($string1) && is_null($string2)) {
return true;
}
if ((is_null($string1) && !is_null($string2)) || (!is_null($string1) && is_null($string2))) {
return false;
}
Expand All @@ -186,6 +189,9 @@ public static function remove(?string $string, ?string $toRemove): ?string
if (is_null($string)) {
return null;
}
if (is_null($toRemove)) {
return $string;
}
return str_replace($toRemove, self::EMPTY_STRING, $string);
}

Expand Down Expand Up @@ -405,6 +411,9 @@ public static function abbreviate(?string $string, ?string $maxWidth): ?string
*/
public static function trimToNull(?string $string): ?string
{
if (is_null($string)) {
return null;
}
$string = trim($string);
if (mb_strlen($string) == 0) {
return null;
Expand Down Expand Up @@ -488,7 +497,7 @@ public static function containsIgnoreCase(?string $string, ?string $substring):

public static function substringBefore(?string $string, ?string $separator): ?string
{
if (is_null($separator)) {
if (is_null($separator) || is_null($string)) {
return $string;
}
$pos = mb_strpos($string, $separator);
Expand All @@ -497,7 +506,7 @@ public static function substringBefore(?string $string, ?string $separator): ?st

public static function substringAfter(?string $string, ?string $separator): ?string
{
if (is_null($separator)) {
if (is_null($separator) || is_null($string)) {
return $string;
}
$pos = mb_strpos($string, $separator);
Expand All @@ -509,7 +518,7 @@ public static function substringAfter(?string $string, ?string $separator): ?str

public static function replaceNth(?string $subject, ?string $search, ?string $replace, int $nth): ?string
{
if (is_null($search)) {
if (is_null($search) || is_null($subject)) {
return $subject;
}
$found = preg_match_all('/' . $search . '/', $subject, $matches, PREG_OFFSET_CAPTURE);
Expand Down
5 changes: 3 additions & 2 deletions test/src/Ouzo/Core/FrontControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,17 @@ public function shouldAddSomeExampleFieldIntoRequestObject()
{
//given
Route::get('/sample/save', SampleFrontController::class, 'save');
$interceptor = new SampleMiddleware();
$middlewareRepository = new MiddlewareRepository();
$middlewareRepository->add(new SampleMiddleware());
$middlewareRepository->add($interceptor);

$this->injectorConfig->bind(MiddlewareRepository::class)->toInstance($middlewareRepository);

//when
$this->get('/sample/save');

//then
$this->assertEquals('SampleMiddleware', $this->requestContext()->forTestPurposesOnly);
$this->assertEquals('SampleMiddleware', $interceptor->getData());
$this->assertRenderedContent()->isEqualTo('save');
}

Expand Down
6 changes: 3 additions & 3 deletions test/src/Ouzo/Core/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,10 @@ public function shouldSerializeModel()
$product = new Product(['name' => 'Sport']);

// when
$serialized = $product->serialize();
$serialized = $product->__serialize();

// then
$this->assertEquals('a:1:{s:4:"name";s:5:"Sport";}', $serialized);
$this->assertEquals(['name' => 'Sport'], $serialized);
}

/**
Expand All @@ -945,7 +945,7 @@ public function shouldUnserializeModel()
$product = new Product(['name' => 'Test']);

// when
$product->unserialize('a:1:{s:4:"name";s:5:"Sport";}');
$product->__unserialize(['name' => 'Sport']);

// then
$this->assertEquals('Sport', $product->name);
Expand Down
9 changes: 8 additions & 1 deletion test/src/Ouzo/Core/SampleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@

class SampleMiddleware implements Interceptor
{
private string $data;

public function handle(mixed $param, Chain $next): mixed
{
$param->forTestPurposesOnly = 'SampleMiddleware';
$this->data = 'SampleMiddleware';
return $next->proceed($param);
}

public function getData(): string
{
return $this->data;
}
}
Loading

0 comments on commit 3073dc5

Please sign in to comment.