Skip to content

Commit

Permalink
Merge pull request #315 from clue-labs/drop-connector-alternative
Browse files Browse the repository at this point in the history
Drop deprecated alternative `Connector` constructor argument order
  • Loading branch information
WyriHaximus authored May 31, 2024
2 parents 874deeb + e7e3b55 commit a2b4fe1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 106 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1150,18 +1150,6 @@ here in order to use the [default loop](https://github.com/reactphp/event-loop#l
This value SHOULD NOT be given unless you're sure you want to explicitly use a
given event loop instance.

> Changelog v1.9.0: The constructur signature has been updated to take the
> optional `$context` as the first parameter and the optional `$loop` as a second
> argument. The previous signature has been deprecated and should not be used anymore.
>
> ```php
> // constructor signature as of v1.9.0
> $connector = new React\Socket\Connector(array $context = [], ?LoopInterface $loop = null);
>
> // legacy constructor signature before v1.9.0
> $connector = new React\Socket\Connector(?LoopInterface $loop = null, array $context = []);
> ```
### Advanced client usage

#### TcpConnector
Expand Down
21 changes: 3 additions & 18 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ final class Connector implements ConnectorInterface
* This class takes two optional arguments for more advanced usage:
*
* ```php
* // constructor signature as of v1.9.0
* $connector = new React\Socket\Connector(array $context = [], ?LoopInterface $loop = null);
*
* // legacy constructor signature before v1.9.0
* $connector = new React\Socket\Connector(?LoopInterface $loop = null, array $context = []);
* ```
*
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
Expand All @@ -49,23 +45,12 @@ final class Connector implements ConnectorInterface
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
* given event loop instance.
*
* @param array|LoopInterface|null $context
* @param null|LoopInterface|array $loop
* @param array $context
* @param ?LoopInterface $loop
* @throws \InvalidArgumentException for invalid arguments
*/
public function __construct($context = array(), $loop = null)
public function __construct(array $context = array(), LoopInterface $loop = null)
{
// swap arguments for legacy constructor signature
if (($context instanceof LoopInterface || $context === null) && (\func_num_args() <= 1 || \is_array($loop))) {
$swap = $loop === null ? array(): $loop;
$loop = $context;
$context = $swap;
}

if (!\is_array($context) || ($loop !== null && !$loop instanceof LoopInterface)) {
throw new \InvalidArgumentException('Expected "array $context" and "?LoopInterface $loop" arguments');
}

// apply default options if not explicitly given
$context += array(
'tcp' => true,
Expand Down
76 changes: 0 additions & 76 deletions tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,82 +56,6 @@ public function testConstructWithContextAssignsGivenContext()
$this->assertSame($tcp, $connectors['tcp']);
}

public function testConstructWithLegacyContextSignatureAssignsGivenContext()
{
$tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();

$connector = new Connector(null, array(
'tcp' => $tcp,
'dns' => false,
'timeout' => false
));

$ref = new \ReflectionProperty($connector, 'connectors');
$ref->setAccessible(true);
$connectors = $ref->getValue($connector);

$this->assertSame($tcp, $connectors['tcp']);
}

public function testConstructWithLegacyLoopSignatureAssignsGivenLoop()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$connector = new Connector($loop);

$ref = new \ReflectionProperty($connector, 'connectors');
$ref->setAccessible(true);
$connectors = $ref->getValue($connector);

$ref = new \ReflectionProperty($connectors['tcp'], 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($connectors['tcp']);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}

public function testConstructWithInvalidContextThrows()
{
$this->setExpectedException('InvalidArgumentException');
new Connector('foo');
}

public function testConstructWithInvalidLoopThrows()
{
$this->setExpectedException('InvalidArgumentException');
new Connector(array(), 'foo');
}

public function testConstructWithContextTwiceThrows()
{
$this->setExpectedException('InvalidArgumentException');
new Connector(array(), array());
}

public function testConstructWithLoopTwiceThrows()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$this->setExpectedException('InvalidArgumentException');
new Connector($loop, $loop);
}

public function testConstructWithNullContextAndLoopThrows()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$this->setExpectedException('InvalidArgumentException');
new Connector(null, $loop);
}

public function testConstructWithLoopAndNullContextThrows()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$this->setExpectedException('InvalidArgumentException');
new Connector($loop, null);
}

public function testConnectorUsesTcpAsDefaultScheme()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
Expand Down

0 comments on commit a2b4fe1

Please sign in to comment.