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

Drop deprecated alternative Connector constructor argument order #315

Merged
merged 1 commit into from
May 31, 2024
Merged
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
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1156,18 +1156,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)
clue marked this conversation as resolved.
Show resolved Hide resolved
{
// 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