-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Description
Hi,
Currently I am not aware of any PSR restrictions on multiple namespace declarations. After running CodeSniffer against my unit tests, I noticed the following error.
> phpcs --standard=PSR1,PSR2 src/ test/
FILE: /mnt/c/Users/admin/www/My/Project/test/Http/ClientTest.php
----------------------------------------------------------------------
FOUND 12 ERRORS AFFECTING 12 LINES
----------------------------------------------------------------------
51 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
52 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
53 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
54 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
55 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
56 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
57 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
58 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
59 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
60 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
61 | ERROR | [ ] USE declarations must go after the first namespace
| | declaration
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 1.31 secs; Memory: 10MB
The code in question is using multiple bracketed namespaces to mock built-in PHP functions, as I am unaware of any other way to do this reliably.
<?php
namespace My\Project\Http {
use My\Project\Test\Http\ClientTest;
use ReflectionException;
use ReflectionObject;
/**
* @param resource $mh
* @param int $still_running
*
* @return int
* @throws ReflectionException
*/
function curl_multi_exec($mh, &$still_running)
{
$still_running = 1;
$ref = new ReflectionObject(ClientTest::$client);
$method = $ref->getMethod("writeHeader");
$method->setAccessible(true);
$method->invoke(ClientTest::$client, ClientTest::$handle, "HTTP/1.1 200 OK\r\n");
$method->invoke(ClientTest::$client, ClientTest::$handle, "Content-Type: application/json\r\n");
$method = $ref->getMethod("writeBody");
$method->setAccessible(true);
$method->invoke(ClientTest::$client, ClientTest::$handle, "{}");
return CURLM_OK;
}
/**
* @param resource $mh
* @param int|null $msgs_in_queue
*
* @return array
*/
function curl_multi_info_read($mh, &$msgs_in_queue = null)
{
return array(
"msg" => CURLMSG_DONE,
"result" => ClientTest::$result,
"handle" => ClientTest::$handle
);
}
}
namespace My\Project\Test\Http {
use My\Project\Futures\PromiseInterface;
use My\Project\Http\Client;
use My\Project\Http\Exceptions\MethodNotAllowedException;
use My\Project\Http\Request;
use Exception;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionObject;
use Slim\Http\Headers;
use Slim\Http\RequestBody;
use Slim\Http\Uri;
class ClientTest extends TestCase
{
/** @var Client $client */
public static $client;
/** @var int $result */
public static $result = CURLE_OK;
/** @var resource|null $handle */
public static $handle = null;
/**
* @throws MethodNotAllowedException
* @throws ReflectionException
* @throws Exception
*/
public function testSendSuccess()
{
$request = new Request(
"GET",
Uri::createFromString("/"),
new Headers(),
array(),
array("SERVER_PROTOCOL" => "1.1"),
new RequestBody()
);
static::$client = new Client();
$promise = static::$client->send($request);
$this->assertInstanceOf(PromiseInterface::class, $promise);
$ref = new ReflectionObject(static::$client);
$property = $ref->getProperty("promises");
$property->setAccessible(true);
$promises = $property->getValue(static::$client);
$key = array_search($promise, $promises, true);
$this->assertNotFalse($key);
$property = $ref->getProperty("handles");
$property->setAccessible(true);
$handles = $property->getValue(static::$client);
static::$handle = $handles[$key];
$promise->wait();
}
public function testGetConfigSuccess()
{
$client = new Client();
$this->assertIsArray($client->getConfig());
$this->assertIsInt($client->getConfig("cache_size"));
$this->assertNull($client->getConfig("invalid"));
}
}
}
Reactions are currently unavailable