Skip to content

Commit

Permalink
Fix BC break in userland classes extending AbstractRequest due to a…
Browse files Browse the repository at this point in the history
… final constructor

* Closes #56
* See also #55
* Remove the final keyword from AbstractRequest::__construct
* Remove the named constructor ::newWithRequestContent() from AbstractRequest
* Remove the named constructor ::newWithRequestContent() from interface ProvidesRequestData in order avoid required implementation in userland derived classes from AbstractRequest.
* Add named constructor ::newWithRequestContent() to all derived request classes separately in order to avoid new static() calls.
  • Loading branch information
hollodotme committed May 6, 2020
1 parent 3dd8e8f commit b566ed0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 61 deletions.
8 changes: 0 additions & 8 deletions src/Interfaces/ProvidesRequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
*/
interface ProvidesRequestData
{
/**
* @param string $scriptFilename
* @param ComposesRequestContent $requestContent
*
* @return static
*/
public static function newWithRequestContent( string $scriptFilename, ComposesRequestContent $requestContent );

public function getGatewayInterface() : string;

public function getRequestMethod() : string;
Expand Down
17 changes: 1 addition & 16 deletions src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\ServerProtocol;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;
use hollodotme\FastCGI\Interfaces\ProvidesRequestData;
use function strlen;

Expand Down Expand Up @@ -85,26 +84,12 @@ abstract class AbstractRequest implements ProvidesRequestData
/** @var array<callable> */
private $passThroughCallbacks = [];

final public function __construct( string $scriptFilename, string $content )
public function __construct( string $scriptFilename, string $content )
{
$this->scriptFilename = $scriptFilename;
$this->setContent( $content );
}

/**
* @param string $scriptFilename
* @param ComposesRequestContent $requestContent
*
* @return static
*/
public static function newWithRequestContent( string $scriptFilename, ComposesRequestContent $requestContent )
{
$instance = new static( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getServerSoftware() : string
{
return $this->serverSoftware;
Expand Down
14 changes: 13 additions & 1 deletion src/Requests/DeleteRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2016-2020 Holger Woltersdorf & Contributors
Expand All @@ -24,13 +24,25 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\RequestMethod;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;

/**
* Class DeleteRequest
* @package hollodotme\FastCGI\Requests
*/
class DeleteRequest extends AbstractRequest
{
public static function newWithRequestContent(
string $scriptFilename,
ComposesRequestContent $requestContent
) : DeleteRequest
{
$instance = new self( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getRequestMethod() : string
{
return RequestMethod::DELETE;
Expand Down
14 changes: 13 additions & 1 deletion src/Requests/GetRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2016-2020 Holger Woltersdorf & Contributors
Expand All @@ -24,13 +24,25 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\RequestMethod;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;

/**
* Class GetRequest
* @package hollodotme\FastCGI\Requests
*/
class GetRequest extends AbstractRequest
{
public static function newWithRequestContent(
string $scriptFilename,
ComposesRequestContent $requestContent
) : GetRequest
{
$instance = new self( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getRequestMethod() : string
{
return RequestMethod::GET;
Expand Down
14 changes: 13 additions & 1 deletion src/Requests/PatchRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2016-2020 Holger Woltersdorf & Contributors
Expand All @@ -24,13 +24,25 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\RequestMethod;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;

/**
* Class PatchRequest
* @package hollodotme\FastCGI\Requests
*/
class PatchRequest extends AbstractRequest
{
public static function newWithRequestContent(
string $scriptFilename,
ComposesRequestContent $requestContent
) : PatchRequest
{
$instance = new self( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getRequestMethod() : string
{
return RequestMethod::PATCH;
Expand Down
14 changes: 13 additions & 1 deletion src/Requests/PostRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2016-2020 Holger Woltersdorf & Contributors
Expand All @@ -24,13 +24,25 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\RequestMethod;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;

/**
* Class PostRequest
* @package hollodotme\FastCGI\Requests
*/
class PostRequest extends AbstractRequest
{
public static function newWithRequestContent(
string $scriptFilename,
ComposesRequestContent $requestContent
) : PostRequest
{
$instance = new self( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getRequestMethod() : string
{
return RequestMethod::POST;
Expand Down
14 changes: 13 additions & 1 deletion src/Requests/PutRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/*
* Copyright (c) 2010-2014 Pierrick Charron
* Copyright (c) 2016-2020 Holger Woltersdorf & Contributors
Expand All @@ -24,13 +24,25 @@
namespace hollodotme\FastCGI\Requests;

use hollodotme\FastCGI\Constants\RequestMethod;
use hollodotme\FastCGI\Interfaces\ComposesRequestContent;

/**
* Class PutRequest
* @package hollodotme\FastCGI\Requests
*/
class PutRequest extends AbstractRequest
{
public static function newWithRequestContent(
string $scriptFilename,
ComposesRequestContent $requestContent
) : PutRequest
{
$instance = new self( $scriptFilename, $requestContent->getContent() );
$instance->setContentType( $requestContent->getContentType() );

return $instance;
}

public function getRequestMethod() : string
{
return RequestMethod::PUT;
Expand Down
52 changes: 20 additions & 32 deletions tests/Unit/Requests/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@
namespace hollodotme\FastCGI\Tests\Unit\Requests;

use hollodotme\FastCGI\Requests\AbstractRequest;
use hollodotme\FastCGI\Requests\DeleteRequest;
use hollodotme\FastCGI\Requests\GetRequest;
use hollodotme\FastCGI\Requests\PatchRequest;
use hollodotme\FastCGI\Requests\PostRequest;
use hollodotme\FastCGI\Requests\PutRequest;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

final class AbstractRequestTest extends TestCase
Expand All @@ -40,7 +34,7 @@ final class AbstractRequestTest extends TestCase
* @param string $requestMethod
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException|RuntimeException
* @throws InvalidArgumentException
* @dataProvider requestMethodProvider
*/
public function testCanGetDefaultValues( string $requestMethod ) : void
Expand Down Expand Up @@ -70,30 +64,24 @@ public function testCanGetDefaultValues( string $requestMethod ) : void
* @param string $content
*
* @return AbstractRequest
* @throws RuntimeException
*/
private function getRequest( string $requestMethod, string $scriptFilename, string $content ) : AbstractRequest
{
switch ( $requestMethod )
{
case 'GET':
return new GetRequest( $scriptFilename, $content );

case 'POST':
return new PostRequest( $scriptFilename, $content );

case 'PUT':
return new PutRequest( $scriptFilename, $content );

case 'PATCH':
return new PatchRequest( $scriptFilename, $content );

case 'DELETE':
return new DeleteRequest( $scriptFilename, $content );

default:
throw new RuntimeException( 'Method not implemented.' );
}
return new class($requestMethod, $scriptFilename, $content) extends AbstractRequest {
/** @var string */
private $requestMethod;

public function __construct( string $requestMethod, string $scriptFilename, string $content )
{
parent::__construct( $scriptFilename, $content );
$this->requestMethod = $requestMethod;
}

public function getRequestMethod() : string
{
return $this->requestMethod;
}
};
}

public function requestMethodProvider() : array
Expand All @@ -111,7 +99,7 @@ public function requestMethodProvider() : array
* @param string $requestMethod
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException|RuntimeException
* @throws InvalidArgumentException
* @dataProvider requestMethodProvider
*/
public function testCanGetParametersArray( string $requestMethod ) : void
Expand Down Expand Up @@ -142,7 +130,7 @@ public function testCanGetParametersArray( string $requestMethod ) : void

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException|RuntimeException
* @throws InvalidArgumentException
*/
public function testContentLengthChangesWithContent() : void
{
Expand All @@ -157,7 +145,7 @@ public function testContentLengthChangesWithContent() : void

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException|RuntimeException
* @throws InvalidArgumentException
*/
public function testCanOverwriteVars() : void
{
Expand Down Expand Up @@ -200,7 +188,7 @@ public function testCanOverwriteVars() : void

/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException|RuntimeException
* @throws InvalidArgumentException
*/
public function testCanResetCustomVars() : void
{
Expand Down

0 comments on commit b566ed0

Please sign in to comment.