diff --git a/.phpstan-baseline.php b/.phpstan-baseline.php index 484d5a7c550..52fa890dd9f 100644 --- a/.phpstan-baseline.php +++ b/.phpstan-baseline.php @@ -1999,18 +1999,6 @@ 'count' => 1, 'path' => __DIR__ . '/src/GLPINetwork.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#', - 'identifier' => 'function.alreadyNarrowedType', - 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Agent/Communication/AbstractRequest.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Property Glpi\\\\Agent\\\\Communication\\\\AbstractRequest\\:\\:\\$response \\(DOMDocument\\) does not accept array\\.$#', - 'identifier' => 'assign.propertyType', - 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Agent/Communication/AbstractRequest.php', -]; $ignoreErrors[] = [ 'message' => '#^Dead catch \\- Glpi\\\\Exception\\\\PasswordTooWeakException is never thrown in the try block\\.$#', 'identifier' => 'catch.neverThrown', @@ -3505,12 +3493,6 @@ 'count' => 2, 'path' => __DIR__ . '/src/Glpi/Inventory/Inventory.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Call to function is_array\\(\\) with array will always evaluate to true\\.$#', - 'identifier' => 'function.alreadyNarrowedType', - 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Inventory/Request.php', -]; $ignoreErrors[] = [ 'message' => '#^Call to function in_array\\(\\) with arguments null, array\\{\'development\', \'testing\'\\} and true will always evaluate to false\\.$#', 'identifier' => 'function.impossibleType', diff --git a/phpunit/functional/Glpi/Inventory/RequestTest.php b/phpunit/functional/Glpi/Inventory/RequestTest.php index 82840af6a55..44aa29a575a 100644 --- a/phpunit/functional/Glpi/Inventory/RequestTest.php +++ b/phpunit/functional/Glpi/Inventory/RequestTest.php @@ -109,8 +109,12 @@ public function testSnmpQuery($query) $request = $this->getMockBuilder(\Glpi\Inventory\Request::class) ->onlyMethods(['inventory', 'prolog']) ->getMock(); - $request->method('inventory')->willReturn(null); - $request->method('prolog')->willReturn(null); + $request->method('inventory')->willReturnCallback(function () { + return; + }); + $request->method('prolog')->willReturnCallback(function () { + return; + }); $request->handleContentType('Application/xml'); $request->handleRequest($data); diff --git a/src/Glpi/Agent/Communication/AbstractRequest.php b/src/Glpi/Agent/Communication/AbstractRequest.php index a61123a03dc..bb9f0678115 100644 --- a/src/Glpi/Agent/Communication/AbstractRequest.php +++ b/src/Glpi/Agent/Communication/AbstractRequest.php @@ -94,24 +94,24 @@ abstract class AbstractRequest const COMPRESS_BR = 3; const COMPRESS_DEFLATE = 4; - /** @var integer */ - protected $mode; + /** @var ?integer */ + protected ?int $mode = null; /** @var string */ - private $deviceid; - /** @var DOMDocument */ - private $response; - /** @var integer */ - private $compression; + private string $deviceid; + /** @var DOMDocument|array|null */ + private DOMDocument|array|null $response = null; + /** @var ?integer */ + private ?int $compression = null; /** @var boolean */ - private $error = false; + private bool $error = false; /** @var boolean */ - protected $test_rules = false; - /** @var \Glpi\Agent\Communication\Headers\Common */ - protected $headers; + protected bool $test_rules = false; + /** @var Common */ + protected Common $headers; /** @var int */ - private $http_response_code = 200; + private int $http_response_code = 200; /** @var string */ - protected $query; + protected string $query; public function __construct() { @@ -130,7 +130,7 @@ abstract protected function initHeaders(): Common; * * @throw RuntimeException */ - protected function setMode($mode) + protected function setMode(int $mode): void { $this->mode = $mode; switch ($mode) { @@ -152,9 +152,11 @@ protected function setMode($mode) /** * Guess import mode * + * @param mixed $contents + * * @return void */ - private function guessMode($contents): void + private function guessMode(mixed $contents): void { // In the case handleContentType() didn't set mode, just check $contents first char if ($contents[0] === '{') { @@ -168,7 +170,8 @@ private function guessMode($contents): void /** * Display module name * - * @param string $internalModule + * @param ?string $internalModule + * * @return string readable method name */ public static function getModuleName(?string $internalModule): string @@ -191,9 +194,9 @@ public static function getModuleName(?string $internalModule): string /** * Handle request headers * - * @param $data + * @return void */ - public function handleHeaders() + public function handleHeaders(): void { $req_headers = getallheaders(); $this->headers->setHeaders($req_headers); @@ -206,7 +209,7 @@ public function handleHeaders() * * @return boolean */ - public function handleRequest($data): bool + public function handleRequest(mixed $data): bool { $auth_required = \Config::getConfigurationValue('inventory', 'auth_required'); if ($auth_required === Conf::CLIENT_CREDENTIALS) { @@ -256,7 +259,7 @@ public function handleRequest($data): bool } } - // Some network inventories may request may contains lots of information. + // Some network inventories may request may contain lots of information. // e.g. a Huawei S5720-52X-LI-AC inventory file may weigh 20MB, // and GLPI will consume about 500MB of memory to handle it, // and may take up to 2 minutes on server that has low performances. @@ -302,25 +305,22 @@ public function handleRequest($data): bool } //load and check data - switch ($this->mode) { - case self::XML_MODE: - return $this->handleXMLRequest($data); - case self::JSON_MODE: - return $this->handleJSONRequest($data); - } - - return false; + return match ($this->mode) { + self::XML_MODE => $this->handleXMLRequest($data), + self::JSON_MODE => $this->handleJSONRequest($data), + default => false, + }; } /** * Handle Query * * @param string $action Action (one of self::*_ACTION) - * @param mixed $content Contents, optional + * @param ?mixed $content Contents, optional * * @return boolean */ - abstract protected function handleAction($action, $content = null): bool; + abstract protected function handleAction(string $action, mixed $content = null): bool; /** * Handle Task @@ -329,7 +329,7 @@ abstract protected function handleAction($action, $content = null): bool; * * @return array */ - abstract protected function handleTask($task): array; + abstract protected function handleTask(string $task): array; /** * Handle XML request @@ -338,7 +338,7 @@ abstract protected function handleTask($task): array; * * @return boolean */ - public function handleXMLRequest($data): bool + public function handleXMLRequest(string $data): bool { libxml_use_internal_errors(true); @@ -377,7 +377,7 @@ public function handleXMLRequest($data): bool * * @return boolean */ - public function handleJSONRequest($data): bool + public function handleJSONRequest(string $data): bool { if (!\Toolbox::isJSON($data)) { $this->addError('JSON not well formed!', 400); @@ -402,7 +402,7 @@ public function handleJSONRequest($data): bool * * @return null|integer One of self::*_MODE */ - public function getMode() + public function getMode(): ?int { return $this->mode; } @@ -410,12 +410,12 @@ public function getMode() /** * Adds an error * - * @param string $message Error message + * @param ?string $message Error message * @param integer $code HTTP response code * * @return void */ - public function addError($message, $code = 500) + public function addError(?string $message, int $code = 500): void { if ($code >= 400) { $this->error = true; @@ -448,7 +448,7 @@ public function addError($message, $code = 500) * * @return void */ - public function addToResponse(array $entries) + public function addToResponse(array $entries): void { if ($this->mode === self::XML_MODE) { $root = $this->response->documentElement; @@ -472,12 +472,12 @@ public function addToResponse(array $entries) * Add node to response for XML_MODE * * @param DOMElement $parent Parent element - * @param string $name Element name to create - * @param string|array|null $content Element contents, if any + * @param ?mixed $name Element name to create + * @param array|string|null $content Element contents, if any * * @return void */ - private function addNode(DOMElement $parent, $name, $content) + private function addNode(DOMElement $parent, mixed $name, array|string|null $content): void { if (is_array($content) && !isset($content['content']) && !isset($content['attributes'])) { $node = is_string($name) @@ -544,14 +544,11 @@ public function getContentType(): string } } - switch ($this->mode) { - case self::XML_MODE: - return 'application/xml'; - case self::JSON_MODE: - return 'application/json'; - default: - throw new \RuntimeException("Unknown mode " . $this->mode); - } + return match ($this->mode) { + self::XML_MODE => 'application/xml', + self::JSON_MODE => 'application/json', + default => throw new \RuntimeException("Unknown mode " . $this->mode), + }; } /** @@ -568,16 +565,11 @@ public function getResponse(): string throw new \RuntimeException("Mode has not been set"); } - switch ($this->mode) { - case self::XML_MODE: - $data = trim($this->response->saveXML()); - break; - case self::JSON_MODE: - $data = json_encode($this->response); - break; - default: - throw new \UnexpectedValueException("Unknown mode " . $this->mode); - } + $data = match ($this->mode) { + self::XML_MODE => trim($this->response->saveXML()), + self::JSON_MODE => json_encode($this->response), + default => throw new \UnexpectedValueException("Unknown mode " . $this->mode), + }; if ($this->compression === null) { throw new \RuntimeException("Compression has not been set"); @@ -620,7 +612,7 @@ public function getResponse(): string * * @return void */ - public function handleContentType($type) + public function handleContentType(string $type): void { switch (strtolower($type)) { case 'application/x-zlib': @@ -663,7 +655,7 @@ public function handleContentType($type) * * @return boolean */ - public function inError() + public function inError(): bool { return $this->error; } @@ -698,7 +690,7 @@ public function acceptedEncodings(): array * * @return void */ - private function prepareHeaders() + private function prepareHeaders(): void { $headers = [ 'Content-Type' => $this->getContentType(), @@ -713,7 +705,7 @@ private function prepareHeaders() * * @return array */ - public function getHeaders($legacy = true): array + public function getHeaders(bool $legacy = true): array { return $this->headers->getHeaders($legacy); } diff --git a/src/Glpi/Agent/Communication/Headers/Common.php b/src/Glpi/Agent/Communication/Headers/Common.php index aef2dc9cf0e..e3a4b53391f 100644 --- a/src/Glpi/Agent/Communication/Headers/Common.php +++ b/src/Glpi/Agent/Communication/Headers/Common.php @@ -46,7 +46,7 @@ class Common * * @var string */ - protected $content_type; + protected string $content_type; /** * "Accept" HTTP header @@ -55,7 +55,7 @@ class Common * * @var string */ - protected $accept; + protected string $accept; /** * "Cache-Control" HTTP header @@ -63,7 +63,7 @@ class Common * * @var string */ - protected $cache_control = 'no-cache,no-store'; + protected string $cache_control = 'no-cache,no-store'; /** * "Connection" HTTP header @@ -71,7 +71,7 @@ class Common * * @var string */ - protected $connection = 'close'; + protected string $connection = 'close'; /** * "Pragma" HTTP header @@ -81,7 +81,7 @@ class Common * * @var string */ - protected $pragma = 'no-cache'; + protected string $pragma = 'no-cache'; //GLPI agent headers /** @@ -92,7 +92,7 @@ class Common * * @var string */ - protected $glpi_agent_id; + protected string $glpi_agent_id; /** * "GLPI-Request-ID" HTTP header @@ -101,7 +101,7 @@ class Common * * @var string */ - protected $glpi_request_id; + protected string $glpi_request_id; /** * "GLPI-CryptoKey-ID" HTTP header @@ -110,7 +110,7 @@ class Common * * @var string */ - protected $glpi_cryptokey_id; + protected string $glpi_cryptokey_id; /** * "GLPI-Proxy-ID" HTTP header @@ -119,19 +119,19 @@ class Common * * @var string */ - protected $glpi_proxy_id; + protected string $glpi_proxy_id; /** * Authorization header - * @var string + * @var ?string */ - protected $authorization; + protected ?string $authorization = null; /** * Define HTTP Authorization Header Authorization type * @var string */ - protected $www_authenticate; + protected string $www_authenticate; public function getRequireds(): array { @@ -158,7 +158,7 @@ public function getHeadersNames(): array * * @return array */ - public function getHeaders($legacy = true): array + public function getHeaders(bool $legacy = true): array { //parse class attributes and normalize key name $reflect = new ReflectionClass($this); @@ -188,8 +188,10 @@ public function getHeaders($legacy = true): array * Get header value * * @param string $name Header name + * + * @return mixed */ - public function getHeader($name) + public function getHeader(string $name): mixed { $propname = strtolower(str_replace('-', '_', $name)); @@ -203,7 +205,7 @@ public function getHeader($name) * * @return string */ - final public function getHeaderName($prop): string + final public function getHeaderName(string $prop): string { $name = $prop; @@ -231,11 +233,11 @@ final public function getHeaderName($prop): string /** * Set multiple HTTP header values at once * - * @param $headers Array of HTTP header name as key and value + * @param array $headers Array of HTTP header name as key and value * * @return $this */ - public function setHeaders($headers): self + public function setHeaders(array $headers): self { foreach ($headers as $header => $value) { $this->setHeader($header, $value); @@ -246,12 +248,12 @@ public function setHeaders($headers): self /** * Set HTTP header value * - * @param $name HTTP header name - * @param $value Value to set + * @param string $name HTTP header name + * @param mixed $value Value to set * * @return $this */ - public function setHeader($name, $value): self + public function setHeader(string $name, mixed $value): self { $propname = strtolower(str_replace('-', '_', $name)); if (property_exists($this, $propname)) { @@ -267,7 +269,7 @@ public function setHeader($name, $value): self * * @return bool */ - public function hasHeader($name): bool + public function hasHeader(string $name): bool { $propname = strtolower(str_replace('-', '_', $name)); return property_exists($this, $propname) && !empty($this->$propname); diff --git a/src/Glpi/Inventory/Request.php b/src/Glpi/Inventory/Request.php index ece8368b292..c8c216f12fd 100644 --- a/src/Glpi/Inventory/Request.php +++ b/src/Glpi/Inventory/Request.php @@ -50,7 +50,7 @@ class Request extends AbstractRequest { /** @var Inventory */ - private $inventory; + private Inventory $inventory; /** @var bool */ private bool $is_discovery = false; @@ -67,15 +67,15 @@ protected function initHeaders(): Common /** * Handle Query * - * @param string $query Query mode (one of self::*_QUERY or self::*_ACTION) - * @param mixed $content Contents, optional + * @param string $action Query mode (one of self::*_QUERY or self::*_ACTION) + * @param mixed|null $content Contents, optional * * @return boolean */ - protected function handleAction($query, $content = null): bool + protected function handleAction(string $action, mixed $content = null): bool { - $this->query = $query; - switch ($query) { + $this->query = $action; + switch ($action) { case self::GET_PARAMS: $this->getParams($content); break; @@ -104,7 +104,7 @@ protected function handleAction($query, $content = null): bool case self::DEPLOY_ACTION: case self::WOL_ACTION: default: - $this->addError("Query '$query' is not supported.", 501); + $this->addError("Query '$action' is not supported.", 501); return false; } return true; @@ -117,7 +117,7 @@ protected function handleAction($query, $content = null): bool * * @return array */ - protected function handleTask($task): array + protected function handleTask(string $task): array { $params = [ 'options' => [ @@ -156,7 +156,7 @@ protected function handleTask($task): array * * @return void */ - public function getParams($data) + public function getParams(mixed $data): void { /** @var array $CFG_GLPI */ global $CFG_GLPI; @@ -190,7 +190,7 @@ public function getParams($data) * * @return void */ - public function prolog($data) + public function prolog(mixed $data): void { /** @var array $CFG_GLPI */ global $CFG_GLPI; @@ -231,7 +231,7 @@ public function prolog($data) * * @return void */ - public function networkDiscovery($data) + public function networkDiscovery(mixed $data): void { $this->network_inventory_mode = Hooks::NETWORK_DISCOVERY; $this->is_discovery = true; @@ -246,7 +246,7 @@ public function networkDiscovery($data) * * @return void */ - public function networkInventory($data) + public function networkInventory(mixed $data): void { $this->network_inventory_mode = Hooks::NETWORK_INVENTORY; $this->network($data); @@ -259,7 +259,7 @@ public function networkInventory($data) * * @return void */ - public function network($data) + public function network(mixed $data): void { $this->inventory = new Inventory(); $this->inventory @@ -303,7 +303,7 @@ public function network($data) * * @return void */ - public function contact($data) + public function contact(mixed $data): void { /** @var array $CFG_GLPI */ global $CFG_GLPI; @@ -321,7 +321,7 @@ public function contact($data) if ($raw_data !== null && property_exists($raw_data, 'enabled-tasks')) { foreach ($raw_data->{'enabled-tasks'} as $task) { $handle = $this->handleTask($task); - if (is_array($handle) && count($handle)) { + if (count($handle)) { // Insert related task information under tasks list property $response['tasks'][$task] = $handle; } else { @@ -344,7 +344,7 @@ public function contact($data) * * @return void */ - public function inventory($data) + public function inventory(mixed $data): void { /** @var array $CFG_GLPI */ global $CFG_GLPI; diff --git a/tests/web/Glpi/Inventory/Request.php b/tests/web/Glpi/Inventory/Request.php index f8eb4dbbb67..b136f6541d9 100644 --- a/tests/web/Glpi/Inventory/Request.php +++ b/tests/web/Glpi/Inventory/Request.php @@ -41,8 +41,8 @@ class Request extends \DBTestCase { - private $http_client; - private $base_uri; + private GuzzleHttp\Client $http_client; + private string $base_uri; public function beforeTestMethod($method) {