Skip to content

Commit

Permalink
Fix typing and PHPDoc on Inventory classes
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher authored Nov 19, 2024
1 parent 4fc73dd commit 60b78b8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 121 deletions.
18 changes: 0 additions & 18 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
8 changes: 6 additions & 2 deletions phpunit/functional/Glpi/Inventory/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
116 changes: 54 additions & 62 deletions src/Glpi/Agent/Communication/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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) {
Expand All @@ -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] === '{') {
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -402,20 +402,20 @@ public function handleJSONRequest($data): bool
*
* @return null|integer One of self::*_MODE
*/
public function getMode()
public function getMode(): ?int
{
return $this->mode;
}

/**
* 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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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),
};
}

/**
Expand All @@ -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");
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -663,7 +655,7 @@ public function handleContentType($type)
*
* @return boolean
*/
public function inError()
public function inError(): bool
{
return $this->error;
}
Expand Down Expand Up @@ -698,7 +690,7 @@ public function acceptedEncodings(): array
*
* @return void
*/
private function prepareHeaders()
private function prepareHeaders(): void
{
$headers = [
'Content-Type' => $this->getContentType(),
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 60b78b8

Please sign in to comment.