Skip to content
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

Dev php8 #412

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "klein/klein",
"description": "A lightning fast router for PHP",
"keywords": ["router", "routing", "sinatra", "boilerplate"],
"homepage": "https://github.com/klein/klein.php",
"homepage": "https://github.com/optimacros/klein.php",
"license": "MIT",
"authors": [
{
Expand All @@ -19,12 +19,12 @@
}
],
"require": {
"php": ">=5.3.0"
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"phpunit/php-code-coverage": "^2.2",
"squizlabs/php_codesniffer": "1.4.8"
"phpunit/phpunit": "^9.5",
"phpunit/php-code-coverage": "^9.2",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 11 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Klein Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Klein Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/Klein/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public function cookie(
$value = '',
$expiry = null,
$path = '/',
$domain = null,
$domain = '',
$secure = false,
$httponly = false
) {
Expand Down
12 changes: 6 additions & 6 deletions src/Klein/DataCollection/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function __unset($key)
* @see \IteratorAggregate::getIterator()
* @return ArrayIterator
*/
public function getIterator()
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->attributes);
}
Expand All @@ -368,7 +368,7 @@ public function getIterator()
* @param string $key The name of the parameter to return
* @return mixed
*/
public function offsetGet($key)
public function offsetGet($key): mixed
{
return $this->get($key);
}
Expand All @@ -384,7 +384,7 @@ public function offsetGet($key)
* @param mixed $value The value of the parameter to set
* @return void
*/
public function offsetSet($key, $value)
public function offsetSet($key, $value): void
{
$this->set($key, $value);
}
Expand All @@ -399,7 +399,7 @@ public function offsetSet($key, $value)
* @param string $key The name of the parameter
* @return boolean
*/
public function offsetExists($key)
public function offsetExists($key): bool
{
return $this->exists($key);
}
Expand All @@ -414,7 +414,7 @@ public function offsetExists($key)
* @param string $key The name of the parameter
* @return void
*/
public function offsetUnset($key)
public function offsetUnset($key): void
{
$this->remove($key);
}
Expand All @@ -428,7 +428,7 @@ public function offsetUnset($key)
* @see \Countable::count()
* @return int
*/
public function count()
public function count(): int
{
return count($this->attributes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Klein/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class HttpException extends RuntimeException implements HttpExceptionInterface
*/
public static function createFromCode($code)
{
return new static(null, (int) $code);
return new static('', (int) $code);
}
}
40 changes: 11 additions & 29 deletions src/Klein/Klein.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,16 @@ public function dispatch(

// Set up some variables for matching
$skip_num = 0;
/**
* @var RouteCollection
*/
$matched = $this->routes->cloneEmpty(); // Get a clone of the routes collection, as it may have been injected
$methods_matched = array();
$params = array();
$apc = function_exists('apc_fetch');

// Start output buffering
ob_start();
$this->output_buffer_level = ob_get_level();
// Do not buffering
$this->output_buffer_level = 0;

try {
foreach ($this->routes as $route) {
Expand Down Expand Up @@ -686,14 +688,6 @@ public function dispatch(
if (strcasecmp($req_method, 'HEAD') === 0) {
// HEAD requests shouldn't return a body
$this->response->body('');

while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}
} elseif (self::DISPATCH_NO_CAPTURE === $capture) {
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_flush();
}
}
} catch (LockedResponseException $e) {
// Do nothing, since this is an automated behavior
Expand Down Expand Up @@ -781,12 +775,12 @@ function ($errno, $errstr) use (&$error_string) {
E_NOTICE | E_WARNING
);

if (false === preg_match($regex, null) || !empty($error_string)) {
if (false === preg_match($regex, '') || !empty($error_string)) {
// Remove our temporary error handler
restore_error_handler();

throw new RegularExpressionCompilationException(
$error_string,
$error_string ?? '',
preg_last_error()
);
}
Expand Down Expand Up @@ -947,24 +941,12 @@ protected function error($err)
} else {
$this->response->code(500);

while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw new UnhandledException($msg, $err->getCode(), $err);
}
} catch (Throwable $e) { // PHP 7 compatibility
// Make sure to clean the output buffer before bailing
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw $e;
} catch (Exception $e) { // TODO: Remove this catch block once PHP 5.x support is no longer necessary.
// Make sure to clean the output buffer before bailing
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw $e;
}
Expand All @@ -988,7 +970,7 @@ public function onHttpError($callback)
/**
* Handles an HTTP error exception through our HTTP error callbacks
*
* @param HttpExceptionInterface $http_exception The exception that occurred
* @param HttpExceptionInterface|HttpException $http_exception The exception that occurred
* @param RouteCollection $matched The collection of routes that were matched in dispatch
* @param array $methods_matched The HTTP methods that were matched in dispatch
* @return void
Expand Down Expand Up @@ -1083,7 +1065,7 @@ protected function callAfterDispatchCallbacks()
*/
public function skipThis()
{
throw new DispatchHaltedException(null, DispatchHaltedException::SKIP_THIS);
throw new DispatchHaltedException('', DispatchHaltedException::SKIP_THIS);
}

/**
Expand All @@ -1095,7 +1077,7 @@ public function skipThis()
*/
public function skipNext($num = 1)
{
$skip = new DispatchHaltedException(null, DispatchHaltedException::SKIP_NEXT);
$skip = new DispatchHaltedException('', DispatchHaltedException::SKIP_NEXT);
$skip->setNumberOfSkips($num);

throw $skip;
Expand All @@ -1109,7 +1091,7 @@ public function skipNext($num = 1)
*/
public function skipRemaining()
{
throw new DispatchHaltedException(null, DispatchHaltedException::SKIP_REMAINING);
throw new DispatchHaltedException('', DispatchHaltedException::SKIP_REMAINING);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Klein/ResponseCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function setDomain($domain)
if (null !== $domain) {
$this->domain = (string) $domain;
} else {
$this->domain = $domain;
$this->domain = '';
}

return $this;
Expand Down
22 changes: 21 additions & 1 deletion src/Klein/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static function markdown($str, $args = null)

// Encode our args so we can insert them into an HTML string
foreach ($args as &$arg) {
$arg = htmlentities($arg, ENT_QUOTES, 'UTF-8');
$arg = htmlentities($arg ?? '', ENT_QUOTES, 'UTF-8');
}

// Actually do our markdown conversion
Expand Down Expand Up @@ -385,6 +385,26 @@ public function validateParam($param, $err = null)
return $this->validate($this->request->param($param), $err);
}

/**
* Returns the request object
*
* @return Request|null
*/
public function getRequest(): ?Request
{
return $this->request;
}

/**
* Returns the response object
*
* @return Response|null
*/
public function getResponse(): ?Response
{
return $this->response;
}


/**
* Magic "__isset" method
Expand Down
2 changes: 1 addition & 1 deletion src/Klein/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function __call($method, $args)
if (false === $this->err) {
return $result;
} elseif (false === $result) {
throw new ValidationException($this->err);
throw new ValidationException($this->err ?? '');
}

return $this;
Expand Down
6 changes: 3 additions & 3 deletions tests/Klein/Tests/AbstractKleinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use Klein\Request;
use Klein\Response;
use Klein\Tests\Mocks\HeadersNoOp;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;

/**
* AbstractKleinTest
*
* Base test class for PHP Unit testing
*/
abstract class AbstractKleinTest extends PHPUnit_Framework_TestCase
abstract class AbstractKleinTest extends TestCase
{

/**
Expand All @@ -40,7 +40,7 @@ abstract class AbstractKleinTest extends PHPUnit_Framework_TestCase
*
* @return void
*/
protected function setUp()
protected function setUp(): void
{
// Create a new klein app,
// since we need one pretty much everywhere
Expand Down
12 changes: 12 additions & 0 deletions tests/Klein/Tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Klein\Tests;

use Klein\App;
use Klein\Exceptions\DuplicateServiceException;
use Klein\Exceptions\UnknownServiceException;

/**
* AppTest
Expand Down Expand Up @@ -50,6 +52,11 @@ public function testRegisterFiller()

$app->register($func_name, $this->getTestCallable());

$returned = $app->{$func_name}();

$this->assertNotNull($returned);
$this->assertSame(self::TEST_CALLBACK_MESSAGE, $returned);

return array(
'app' => $app,
'func_name' => $func_name,
Expand All @@ -75,6 +82,8 @@ public function testGet(array $args)
*/
public function testGetBadMethod()
{
$this->expectException(UnknownServiceException::class);

$app = new App();
$app->random_thing_that_doesnt_exist;
}
Expand All @@ -98,6 +107,7 @@ public function testCall(array $args)
*/
public function testCallBadMethod()
{
$this->expectException(\BadMethodCallException::class);
$app = new App();
$app->random_thing_that_doesnt_exist();
}
Expand All @@ -108,6 +118,8 @@ public function testCallBadMethod()
*/
public function testRegisterDuplicateMethod(array $args)
{
$this->expectException(DuplicateServiceException::class);

// Get our vars from our args
extract($args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testGetSetNormalization()
{
$data_collection = new HeaderDataCollection();

$this->assertInternalType('int', $data_collection->getNormalization());
$this->assertIsInt($data_collection->getNormalization());

$data_collection->setNormalization(
HeaderDataCollection::NORMALIZE_TRIM & HeaderDataCollection::NORMALIZE_CASE
Expand Down
Loading