diff --git a/bin/phiremock.php b/bin/phiremock.php index 03a915d..ae5755b 100755 --- a/bin/phiremock.php +++ b/bin/phiremock.php @@ -21,7 +21,7 @@ const IS_SINGLE_COMMAND = true; -if (PHP_SAPI !== 'cli') { +if (\PHP_SAPI !== 'cli') { throw new RuntimeException('This is a standalone CLI application'); } diff --git a/src/Actions/ActionLocator.php b/src/Actions/ActionLocator.php index 7c8e6d0..a5f8329 100644 --- a/src/Actions/ActionLocator.php +++ b/src/Actions/ActionLocator.php @@ -61,7 +61,7 @@ public function __construct(ActionsFactory $factory) public function locate(string $actionIdentifier): ActionInterface { - if (array_key_exists($actionIdentifier, self::ACTION_FACTORY_METHOD_MAP)) { + if (\array_key_exists($actionIdentifier, self::ACTION_FACTORY_METHOD_MAP)) { return $this->factory->{self::ACTION_FACTORY_METHOD_MAP[$actionIdentifier]}(); } throw new InvalidArgumentException(sprintf('Trying to get action using %s. Which is not a valid action name.', var_export($actionIdentifier, true))); diff --git a/src/Actions/ListRequestsAction.php b/src/Actions/ListRequestsAction.php index 123525c..8beee82 100644 --- a/src/Actions/ListRequestsAction.php +++ b/src/Actions/ListRequestsAction.php @@ -67,7 +67,7 @@ public function execute(ServerRequestInterface $request, ResponseInterface $resp public function process(ResponseInterface $response, Expectation $expectation): ResponseInterface { $executions = $this->searchForExecutionsCount($expectation); - $this->logger->debug('Listed ' . count($executions) . ' request matching the expectation'); + $this->logger->debug('Listed ' . \count($executions) . ' request matching the expectation'); return $response ->withStatus(200) diff --git a/src/Actions/SetScenarioStateAction.php b/src/Actions/SetScenarioStateAction.php index 6ec3af9..78bbfb1 100644 --- a/src/Actions/SetScenarioStateAction.php +++ b/src/Actions/SetScenarioStateAction.php @@ -98,7 +98,7 @@ private function parseJsonBody(ServerRequestInterface $request): array } $bodyJson = @json_decode($body, true); - if (JSON_ERROR_NONE !== json_last_error()) { + if (\JSON_ERROR_NONE !== json_last_error()) { throw new \Exception(json_last_error_msg()); } diff --git a/src/Cli/Commands/PhiremockServerCommand.php b/src/Cli/Commands/PhiremockServerCommand.php index 84c2c75..f1c9e9e 100644 --- a/src/Cli/Commands/PhiremockServerCommand.php +++ b/src/Cli/Commands/PhiremockServerCommand.php @@ -81,37 +81,37 @@ protected function configure(): void 'debug', 'd', InputOption::VALUE_NONE, - sprintf(self::DEBUG_HELP_MESSAGE) + self::DEBUG_HELP_MESSAGE ) ->addOption( 'config-path', 'c', InputOption::VALUE_REQUIRED, - sprintf(self::CONFIG_PATH_HELP_MESSAGE) + self::CONFIG_PATH_HELP_MESSAGE ) ->addOption( 'factory-class', 'f', InputOption::VALUE_REQUIRED, - sprintf(self::FACTORY_CLASS_HELP_MESSAGE) + self::FACTORY_CLASS_HELP_MESSAGE ) ->addOption( 'certificate', 't', InputOption::VALUE_REQUIRED, - sprintf(self::CERTIFICATE_HELP_MESSAGE) + self::CERTIFICATE_HELP_MESSAGE ) ->addOption( 'certificate-key', 'k', InputOption::VALUE_REQUIRED, - sprintf(self::CERTIFICATE_KEY_HELP_MESSAGE) + self::CERTIFICATE_KEY_HELP_MESSAGE ) ->addOption( 'cert-passphrase', 's', InputOption::VALUE_REQUIRED, - sprintf(self::PASSPHRASE_HELP_MESSAGE) + self::PASSPHRASE_HELP_MESSAGE ); } @@ -133,6 +133,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + protected function getConfigPath(InputInterface $input): ?Directory + { + $configPath = null; + $configPathOptionValue = $input->getOption('config-path'); + if ($configPathOptionValue) { + $configPath = new Directory($configPathOptionValue); + } + + return $configPath; + } + /** @throws Exception */ private function createPhiremockPathIfNotExists(): void { @@ -190,9 +201,9 @@ private function setUpHandlers(): void $this->logger->debug('Registering shutdown function'); register_shutdown_function($handleTermination); - if (function_exists('pcntl_signal')) { + if (\function_exists('pcntl_signal')) { $this->logger->debug('PCNTL present: Installing signal handlers'); - pcntl_signal(SIGTERM, function () { exit(0); }); + pcntl_signal(\SIGTERM, function () { exit(0); }); } $errorHandler = function ($severity, $message, $file, $line) { @@ -210,14 +221,14 @@ private function setUpHandlers(): void private function isError(int $severity): bool { - return in_array( + return \in_array( $severity, [ - E_COMPILE_ERROR, - E_CORE_ERROR, - E_USER_ERROR, - E_PARSE, - E_ERROR, + \E_COMPILE_ERROR, + \E_CORE_ERROR, + \E_USER_ERROR, + \E_PARSE, + \E_ERROR, ], true ); @@ -230,23 +241,10 @@ private function getCommandLineOptions(InputInterface $input): array foreach (Config::CONFIG_OPTIONS as $configOption) { $optionValue = $input->getOption($configOption); if ($optionValue) { - $cliConfig[$configOption] = (string)$optionValue; + $cliConfig[$configOption] = (string) $optionValue; } } - return $cliConfig; - } - /** - * @param InputInterface $input - * @return Directory|null - */ - protected function getConfigPath(InputInterface $input): ?Directory - { - $configPath = null; - $configPathOptionValue = $input->getOption('config-path'); - if ($configPathOptionValue) { - $configPath = new Directory($configPathOptionValue); - } - return $configPath; + return $cliConfig; } } diff --git a/src/Factory/Factory.php b/src/Factory/Factory.php index 68ad04a..e0e919f 100644 --- a/src/Factory/Factory.php +++ b/src/Factory/Factory.php @@ -85,7 +85,7 @@ public function createLogger(): LoggerInterface if (!$this->factoryCache->has('logger')) { $logger = new Logger('stdoutLogger'); $logLevel = $this->config->isDebugMode() ? Logger::DEBUG : Logger::INFO; - $logger->pushHandler(new StreamHandler(STDOUT, $logLevel)); + $logger->pushHandler(new StreamHandler(\STDOUT, $logLevel)); $this->factoryCache->set('logger', $logger); } diff --git a/src/Http/Implementation/FastRouterHandler.php b/src/Http/Implementation/FastRouterHandler.php index 5c0161b..ddbee6f 100644 --- a/src/Http/Implementation/FastRouterHandler.php +++ b/src/Http/Implementation/FastRouterHandler.php @@ -20,6 +20,7 @@ use FastRoute\Dispatcher; use FastRoute\RouteCollector; +use function FastRoute\simpleDispatcher; use Laminas\Diactoros\Response; use Mcustiel\Phiremock\Common\StringStream; use Mcustiel\Phiremock\Server\Actions\ActionLocator; @@ -29,7 +30,6 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerInterface; use Throwable; -use function FastRoute\simpleDispatcher; class FastRouterHandler implements RequestHandlerInterface { @@ -77,6 +77,7 @@ public function dispatch(ServerRequestInterface $request): ResponseInterface ->locate($routeInfo[1]) ->execute($request, new Response()); } + return new Response( new StringStream( json_encode(['result' => 'ERROR', 'details' => 'Unexpected error: Router returned unexpected info']) diff --git a/src/Utils/ArraysHelper.php b/src/Utils/ArraysHelper.php index 849fc11..ff456a1 100644 --- a/src/Utils/ArraysHelper.php +++ b/src/Utils/ArraysHelper.php @@ -26,12 +26,12 @@ public static function isAssociative(array $array): bool return false; } - return array_keys($array) !== range(0, count($array) - 1); + return array_keys($array) !== range(0, \count($array) - 1); } public static function areRecursivelyEquals(array $array1, array $array2): bool { - if (count($array1) !== count($array2)) { + if (\count($array1) !== \count($array2)) { return false; } @@ -41,7 +41,7 @@ public static function areRecursivelyEquals(array $array1, array $array2): bool public static function arrayIsContained(array $array1, array $array2): bool { foreach ($array1 as $key => $value1) { - if (!array_key_exists($key, $array2)) { + if (!\array_key_exists($key, $array2)) { return false; } if (!self::haveTheSameTypeAndValue($value1, $array2[$key])) { @@ -54,7 +54,7 @@ public static function arrayIsContained(array $array1, array $array2): bool public static function haveTheSameTypeAndValue($value1, $value2): bool { - if (gettype($value1) !== gettype($value2)) { + if (\gettype($value1) !== \gettype($value2)) { return false; } @@ -63,7 +63,7 @@ public static function haveTheSameTypeAndValue($value1, $value2): bool public static function haveTheSameValue($value1, $value2): bool { - if (is_array($value1)) { + if (\is_array($value1)) { if (!self::areRecursivelyEquals($value1, $value2)) { return false; } diff --git a/src/Utils/Config/ConfigBuilder.php b/src/Utils/Config/ConfigBuilder.php index be7aa02..9d57c45 100644 --- a/src/Utils/Config/ConfigBuilder.php +++ b/src/Utils/Config/ConfigBuilder.php @@ -70,7 +70,7 @@ public static function getDefaultExpectationsDir(): ExpectationsDirectory { return new ExpectationsDirectory( HomePathService::getHomePath()->getFullSubpathAsString( - '.phiremock' . DIRECTORY_SEPARATOR . 'expectations' + '.phiremock' . \DIRECTORY_SEPARATOR . 'expectations' ) ); } @@ -102,7 +102,7 @@ protected function searchFileAndGetConfig(): array getcwd() . '/.phiremock', getcwd() . '/.phiremock.dist', HomePathService::getHomePath()->getFullSubpathAsString( - '.phiremock' . DIRECTORY_SEPARATOR . 'config' + '.phiremock' . \DIRECTORY_SEPARATOR . 'config' ), '.phiremock', '.phiremock.dist', @@ -112,6 +112,7 @@ protected function searchFileAndGetConfig(): array return require $configFilePath; } } + return []; } } diff --git a/src/Utils/Config/Directory.php b/src/Utils/Config/Directory.php index a975a21..d53db3f 100644 --- a/src/Utils/Config/Directory.php +++ b/src/Utils/Config/Directory.php @@ -29,7 +29,7 @@ class Directory public function __construct(string $directory) { $this->ensureIsDirectory($directory); - $this->directory = rtrim($directory, DIRECTORY_SEPARATOR); + $this->directory = rtrim($directory, \DIRECTORY_SEPARATOR); } public function asString(): string @@ -39,7 +39,7 @@ public function asString(): string public function getFullSubpathAsString(string $subPath): string { - return $this->directory . DIRECTORY_SEPARATOR . $subPath; + return $this->directory . \DIRECTORY_SEPARATOR . $subPath; } private function ensureIsDirectory(string $directory): void diff --git a/src/Utils/DataStructures/StringObjectArrayMap.php b/src/Utils/DataStructures/StringObjectArrayMap.php index a10bc1d..bf6a142 100644 --- a/src/Utils/DataStructures/StringObjectArrayMap.php +++ b/src/Utils/DataStructures/StringObjectArrayMap.php @@ -38,12 +38,12 @@ public function getIterator() public function set($key, $value) { - if (!is_string($key)) { - throw new InvalidArgumentException('Expected key to be string. Got: ' . gettype($key)); + if (!\is_string($key)) { + throw new InvalidArgumentException('Expected key to be string. Got: ' . \gettype($key)); } - if (!is_object($value)) { - throw new InvalidArgumentException('Expected value to be object. Got: ' . gettype($key)); + if (!\is_object($value)) { + throw new InvalidArgumentException('Expected value to be object. Got: ' . \gettype($key)); } $this->mapData[$key] = $value; } @@ -59,8 +59,8 @@ public function get($key) public function has($key) { - if (!is_string($key)) { - throw new InvalidArgumentException('Expected key to be string. Got: ' . gettype($key)); + if (!\is_string($key)) { + throw new InvalidArgumentException('Expected key to be string. Got: ' . \gettype($key)); } return isset($this->mapData[$key]); diff --git a/src/Utils/FileExpectationsLoader.php b/src/Utils/FileExpectationsLoader.php index 0aaed66..0c7edcb 100644 --- a/src/Utils/FileExpectationsLoader.php +++ b/src/Utils/FileExpectationsLoader.php @@ -55,7 +55,7 @@ public function loadExpectationFromFile(string $fileName): void $this->logger->debug("Loading expectation file $fileName"); $content = file_get_contents($fileName); $data = @json_decode($content, true); - if (JSON_ERROR_NONE !== json_last_error()) { + if (\JSON_ERROR_NONE !== json_last_error()) { throw new Exception(json_last_error_msg()); } $expectation = $this->converterLocator->locate($data)->convert($data); diff --git a/src/Utils/RequestToExpectationMapper.php b/src/Utils/RequestToExpectationMapper.php index 6fd0737..0be4c3c 100644 --- a/src/Utils/RequestToExpectationMapper.php +++ b/src/Utils/RequestToExpectationMapper.php @@ -63,7 +63,7 @@ private function parseJsonBody(ServerRequestInterface $request): array } $bodyJson = @json_decode($body, true); - if (JSON_ERROR_NONE !== json_last_error()) { + if (\JSON_ERROR_NONE !== json_last_error()) { throw new Exception(json_last_error_msg()); } $this->logger->debug('BODY JSON: ' . var_export($bodyJson, true)); diff --git a/tests/acceptance/_support/Helper/AcceptanceV2.php b/tests/acceptance/_support/Helper/AcceptanceV2.php index 9d6e5ec..232ad00 100644 --- a/tests/acceptance/_support/Helper/AcceptanceV2.php +++ b/tests/acceptance/_support/Helper/AcceptanceV2.php @@ -77,7 +77,7 @@ public function getPhiremockRequest(array $request): array public function getPhiremockResponse(string $jsonResponse): string { $parsedExpectations = json_decode($jsonResponse, true); - if (json_last_error() !== JSON_ERROR_NONE) { + if (json_last_error() !== \JSON_ERROR_NONE) { return $jsonResponse; } $v2 = []; diff --git a/tests/acceptance/v1/BodyConditionCest.php b/tests/acceptance/v1/BodyConditionCest.php index eeb391a..a50e542 100644 --- a/tests/acceptance/v1/BodyConditionCest.php +++ b/tests/acceptance/v1/BodyConditionCest.php @@ -223,7 +223,6 @@ public function responseExpectedWhenRequestBodyContainsTest(AcceptanceTester $I) $I->seeResponseEquals('Found'); } - public function responseExpectedWhenRequestBodyCaseInsensitiveEqualsTest(AcceptanceTester $I) { $I->wantTo('see if mocking based in request body case insensitive equality works'); diff --git a/tests/codeception/extensions/ServerControl.php b/tests/codeception/extensions/ServerControl.php index e6d7a22..238163a 100644 --- a/tests/codeception/extensions/ServerControl.php +++ b/tests/codeception/extensions/ServerControl.php @@ -61,7 +61,7 @@ public function suiteAfter() if (!$this->application->isRunning()) { return; } - $this->application->stop(5, SIGTERM); + $this->application->stop(5, \SIGTERM); $this->writeln('Phiremock is stopped'); } }