Skip to content

Commit

Permalink
fix: PHPStan types
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed May 29, 2023
1 parent 7982a87 commit f30b4c3
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 30 deletions.
7 changes: 7 additions & 0 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ private function get_apib(string $filename, ?string $rel_path = null): string
{
$path = $this->file_path($filename, $rel_path);
$file = file_get_contents($path);
if ($file === FALSE) {
throw new ExecutionException("API File not readable: $filename", 1);
}
$matches = [];
preg_match_all('<!-- include\(([\S\s]*?)(\.[a-z]*?)\) -->', $file, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
Expand Down Expand Up @@ -160,6 +163,10 @@ private function get_schema(string $url): string
$result = curl_exec($ch);
curl_close($ch);

if (is_bool($result)) {
throw new ExecutionException("Could not retreive schema from: $url", 1);
}

return $result;
}

Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/Elements/BasicStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ public function string_value(bool $flat = false)
{
if (is_array($this->value)) {
$value_key = rand(0, count($this->value));
if (is_subclass_of($this->value[$value_key], StructureElement::class) && $flat === false) {
if (is_subclass_of($this->value[$value_key], StructureElement::class, false) && $flat === false) {
return $this->value[$value_key]->string_value($flat);
}

return $this->value[$value_key];
}

if (is_subclass_of($this->value, BasicStructureElement::class) && $flat === true) {
if (is_subclass_of($this->value, BasicStructureElement::class, false) && $flat === true) {
return is_array($this->value->value) ? array_keys($this->value->value)[0] : $this->value->value;
}

Expand Down
10 changes: 6 additions & 4 deletions src/PHPDraft/Model/Elements/EnumStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public function parse(?object $object, array &$dependencies): StructureElement
return $this;
}

foreach ($object->attributes->enumerations->content as $sub_item) {
$element = new ElementStructureElement();
$element->parse($sub_item, $dependencies);
$this->value[] = $element;
if (isset($object->attributes->enumerations)) {
foreach ($object->attributes->enumerations->content as $sub_item) {
$element = new ElementStructureElement();
$element->parse($sub_item, $dependencies);
$this->value[] = $element;
}
}

$this->deps = $dependencies;
Expand Down
8 changes: 4 additions & 4 deletions src/PHPDraft/Model/Elements/ObjectStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function __toString(): string
if (is_array($this->value)) {
$return = '';
foreach ($this->value as $object) {
if (is_string($object) || is_subclass_of(get_class($object), StructureElement::class)) {
if (is_string($object) || is_subclass_of($object, StructureElement::class, false)) {
$return .= $object;
}
}
Expand All @@ -165,15 +165,15 @@ public function __toString(): string
return $this->construct_string_return('');
}

if (is_object($this->value) && (self::class === get_class($this->value) || RequestBodyElement::class === get_class($this->value))) {
if (self::class === get_class($this->value) || is_a($this->value, RequestBodyElement::class, false)) {
return $this->construct_string_return('<div class="sub-struct">' . $this->value . '</div>');
}

if (is_object($this->value) && (ArrayStructureElement::class === get_class($this->value))) {
if (is_a($this->value, ArrayStructureElement::class, false)) {
return $this->construct_string_return('<div class="array-struct">' . $this->value . '</div>');
}

if (is_object($this->value) && (EnumStructureElement::class === get_class($this->value))) {
if (is_a($this->value, EnumStructureElement::class, false)) {
return $this->construct_string_return('<div class="enum-struct">' . $this->value . '</div>');
}

Expand Down
3 changes: 2 additions & 1 deletion src/PHPDraft/Model/Elements/RequestBodyElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function print_request(?string $type = 'application/x-www-form-urlencoded
$object = [];
$object[$this->key->value] = $value;

return json_encode($object);
$encoded = json_encode($object);
return is_string($encoded) ? $encoded : '';
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HTTPRequest implements Comparable
/**
* HTTP Headers.
*
* @var array<string, string>
* @var array<int|string, string>
*/
public array $headers = [];

Expand Down Expand Up @@ -69,7 +69,7 @@ class HTTPRequest implements Comparable
/**
* Structure of the request.
*
* @var RequestBodyElement[]|RequestBodyElement
* @var array<RequestBodyElement>|RequestBodyElement
*/
public mixed $struct = [];
/**
Expand Down Expand Up @@ -194,7 +194,7 @@ public function get_curl_command(string $base_url, array $additional = []): stri
$options[] = '--data-binary ' . escapeshellarg($this->body);
} elseif (is_array($this->body) && $this->body !== []) {
$options[] = '--data-binary ' . escapeshellarg(join('', $this->body));
} elseif (is_subclass_of($this->struct, StructureElement::class)) {
} elseif (!is_array($this->struct)) {
foreach ($this->struct->value as $body) {
if (is_null($body) || $body === []) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/HTTPResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class HTTPResponse implements Comparable
/**
* Response headers.
*
* @var array<string, string>
* @var array<int|string, string>
*/
public array $headers = [];

/**
* Response bodies.
*
* @var array<string, string>
* @var array<int|string, string>
*/
public array $content = [];

Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Resource extends HierarchyElement
/**
* URL variables.
*
* @var ObjectStructureElement[]
* @var array<ObjectStructureElement>
*/
public array $url_variables = [];

Expand Down
13 changes: 7 additions & 6 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Transition extends HierarchyElement
/**
* URL variables.
*
* @var StructureElement[]
* @var array<StructureElement>
*/
public array $url_variables = [];

Expand All @@ -51,21 +51,21 @@ class Transition extends HierarchyElement
/**
* The request.
*
* @var HTTPRequest[]
* @var array<HTTPRequest>
*/
public array $requests = [];

/**
* The responses.
*
* @var HTTPResponse[]
* @var array<HTTPResponse>
*/
public array $responses = [];

/**
* Structures used (if any).
*
* @var StructureElement[]
* @var array<StructureElement>
*/
public array $structures = [];

Expand Down Expand Up @@ -176,7 +176,7 @@ public function build_url(string $base_url = '', bool $clean = false): string
}
if ($this->parent->url_variables !== []) {
foreach ($this->parent->url_variables as $item) {
if (!is_subclass_of($item, BasicStructureElement::class)) {
if (!is_subclass_of($item, BasicStructureElement::class, false)) {
continue;
}

Expand Down Expand Up @@ -204,7 +204,8 @@ public function build_url(string $base_url = '', bool $clean = false): string
*/
private function overlap_urls(string $str1, string $str2): false|string
{
if ($overlap = $this->find_overlap($str1, $str2)) {
$overlap = $this->find_overlap($str1, $str2);
if (is_array($overlap)) {
$overlap = $overlap[count($overlap) - 1];
$str1 = substr($str1, 0, -strlen($overlap));
$str2 = substr($str2, strlen($overlap));
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Out/BaseTemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class BaseTemplateRenderer
/**
* The base data of the API.
*
* @var array<string, mixed>
* @var array<int|string, mixed>
*/
protected array $base_data;
/**
Expand Down
12 changes: 9 additions & 3 deletions src/PHPDraft/Out/TemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ public function get(object $object): string
}

$loader = new FilesystemLoader([]);
$loader->addPath(stream_resolve_include_path(dirname($include)));
$loader->addPath(stream_resolve_include_path(dirname($this->find_include_file('default', 'twig'))));
$dirpath = stream_resolve_include_path(dirname($include));
if ($dirpath !== FALSE) {
$loader->addPath($dirpath);
}
$twig_path = stream_resolve_include_path(dirname($this->find_include_file('default', 'twig')));
if ($twig_path !== FALSE) {
$loader->addPath($twig_path);
}

$twig = TwigFactory::get($loader);
$template = $twig->load('main.twig');
Expand Down Expand Up @@ -161,7 +167,7 @@ public function find_include_file(string $template, string $extension = 'twig',
'PHPDraft/Out/HTML/' . $template . DIRECTORY_SEPARATOR . 'main' . ".{$extension}",
];
foreach ($includes as $include) {
if (!stream_resolve_include_path($include)) {
if (stream_resolve_include_path($include) === FALSE) {
continue;
}
return $include;
Expand Down
13 changes: 11 additions & 2 deletions src/PHPDraft/Parse/Drafter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function init(ApibFileParser $apib): BaseParser
public static function location()
{
$returnVal = shell_exec('which drafter 2> /dev/null');
if (!is_string($returnVal)) {
return false;
}

$returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal);

return $returnVal === null || $returnVal === '' ? false : $returnVal;
Expand Down Expand Up @@ -79,10 +83,15 @@ protected function parse(): void
public static function available(): bool
{
$path = self::location();
if ($path === FALSE) {
return FALSE;
}

$version = shell_exec('drafter -v 2> /dev/null');
$version = preg_match('/^v([45])/', $version);
if (!is_string($version)) {
return FALSE;
}

return $path && $version === 1;
return preg_match('/^v([45])/', $version) === 1;
}
}
2 changes: 1 addition & 1 deletion src/PHPDraft/Parse/DrafterAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function parse(): void

$response = curl_exec($ch);

if (curl_errno($ch) !== 0) {
if (curl_errno($ch) !== 0 || is_bool($response)) {
throw new ResourceException('Drafter webservice failed to parse input', 1);
}

Expand Down

0 comments on commit f30b4c3

Please sign in to comment.