Skip to content

Commit

Permalink
enable strict_types in generator (#481)
Browse files Browse the repository at this point in the history
(Continuing with pulling the changes from shish/safe into tcm/safe in small hopefully easy-to-review chunks)
  • Loading branch information
shish authored Dec 2, 2024
1 parent 6343b3e commit b35aab3
Show file tree
Hide file tree
Showing 32 changed files with 101 additions and 99 deletions.
1 change: 1 addition & 0 deletions generator/src/ComposerJsonEditor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe;

Expand Down
3 changes: 2 additions & 1 deletion generator/src/DeprecateCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe;

Expand All @@ -23,7 +24,7 @@ protected function configure(): void
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string $moduleName */
$moduleName = $input->getArgument('module');
Expand Down
6 changes: 2 additions & 4 deletions generator/src/DocPage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

use function explode;
Expand Down Expand Up @@ -226,8 +228,6 @@ public function getMethodSynopsis(): array

/**
* Loads the XML file, resolving all DTD declared entities.
*
* @return \SimpleXMLElement
*/
public function loadAndResolveFile(): \SimpleXMLElement
{
Expand Down Expand Up @@ -258,8 +258,6 @@ public function loadAndResolveFile(): \SimpleXMLElement

/**
* Returns the module name in Camelcase.
*
* @return string
*/
public function getModule(): string
{
Expand Down
1 change: 1 addition & 0 deletions generator/src/EmptyTypeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe;

Expand Down
8 changes: 2 additions & 6 deletions generator/src/FileCreator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

use Rector\Config\RectorConfig;
Expand All @@ -14,7 +16,6 @@ class FileCreator
* This function generate an improved php lib function in a php file
*
* @param Method[] $functions
* @param string $path
*/
public function generatePhpFile(array $functions, string $path): void
{
Expand Down Expand Up @@ -64,7 +65,6 @@ private function getFunctionsNameList(array $functions): array
* This function generate a PHP file containing the list of functions we can handle.
*
* @param Method[] $functions
* @param string $path
*/
public function generateFunctionsList(array $functions, string $path): void
{
Expand All @@ -86,7 +86,6 @@ public function generateFunctionsList(array $functions, string $path): void
* Generates a configuration file for replacing all functions when using rector/rector.
*
* @param Method[] $functions
* @param string $path
*/
public function generateRectorFile(array $functions, string $path): void
{
Expand Down Expand Up @@ -148,9 +147,6 @@ public static function createFromPhpError(): self

/**
* Generates the name of the exception class
*
* @param string $moduleName
* @return string
*/
public static function toExceptionName(string $moduleName): string
{
Expand Down
3 changes: 2 additions & 1 deletion generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe;

Expand All @@ -18,7 +19,7 @@ protected function configure(): void
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{

$this->rmGenerated();
Expand Down
34 changes: 9 additions & 25 deletions generator/src/Method.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

use Safe\PhpStanFunctions\PhpStanFunction;
Expand All @@ -11,35 +13,19 @@ class Method
const FALSY_TYPE = 1;
const NULLSY_TYPE = 2;
const EMPTY_TYPE = 3;
/**
* @var \SimpleXMLElement
*/
private $functionObject;
/**
* @var \SimpleXMLElement
*/
private $rootEntity;
/**
* @var string
*/
private $moduleName;
private \SimpleXMLElement $functionObject;
private \SimpleXMLElement $rootEntity;
private string $moduleName;
/**
* @var Parameter[]|null
*/
private $params = null;
/**
* @var int
*/
private $errorType;
private ?array $params = null;
private int $errorType;
/**
* The function prototype from the phpstan internal documentation (functionMap.php)
* @var PhpStanFunction|null
*/
private $phpstanSignarure;
/**
* @var PhpStanType
*/
private $returnType;
private ?PhpStanFunction $phpstanSignarure;
private PhpStanType $returnType;

public function __construct(\SimpleXMLElement $_functionObject, \SimpleXMLElement $rootEntity, string $moduleName, PhpStanFunctionMapReader $phpStanFunctionMapReader, int $errorType)
{
Expand Down Expand Up @@ -225,8 +211,6 @@ public function getModuleName(): string

/**
* The function is overloaded if at least one parameter is optional with no default value and this parameter is not by reference.
*
* @return bool
*/
public function isOverloaded(): bool
{
Expand Down
5 changes: 3 additions & 2 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

use Safe\PhpStanFunctions\PhpStanFunction;
Expand Down Expand Up @@ -53,8 +56,6 @@ public function isByReference(): bool
/**
* Some parameters can be optional with no default value. In this case, the function is "overloaded" (which is not
* possible in user-land but possible in core...)
*
* @return bool
*/
public function isOptionalWithNoDefault(): bool
{
Expand Down
3 changes: 2 additions & 1 deletion generator/src/PhpStanFunctions/PhpStanFunction.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe\PhpStanFunctions;

Expand All @@ -13,7 +14,7 @@ class PhpStanFunction
/**
* @var PhpStanParameter[]
*/
private $parameters = [];
private array $parameters = [];

/**
* @param string[] $signature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe\PhpStanFunctions;

Expand Down
14 changes: 3 additions & 11 deletions generator/src/PhpStanFunctions/PhpStanParameter.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
<?php

declare(strict_types=1);

namespace Safe\PhpStanFunctions;

use Safe\Type;

class PhpStanParameter
{
/**
* @var string
*/
private $name;
/**
* @var PhpStanType
*/
private $type;
private string $name;
private PhpStanType $type;

public function __construct(string $name, string $type)
{
Expand All @@ -29,9 +24,6 @@ public function __construct(string $name, string $type)
$this->type = new PhpStanType($type, $writeOnly);
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
Expand Down
22 changes: 10 additions & 12 deletions generator/src/PhpStanFunctions/PhpStanType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe\PhpStanFunctions;

Expand All @@ -11,24 +12,21 @@
*/
class PhpStanType
{
const NO_SIGNATURE_TYPES = [
public const NO_SIGNATURE_TYPES = [
'resource',
'mixed',
'\OCI-Lob',
'\OCI-Collection',
];
/**
* @var bool
*/
private $nullable;
/**
* @var bool
*/
private $falsable;

private bool $nullable;

private bool $falsable;

/**
* @var string[]
*/
private $types;
private array $types;

public function __construct(string $data, bool $writeOnly = false)
{
Expand All @@ -49,12 +47,12 @@ public function __construct(string $data, bool $writeOnly = false)

$returnTypes = $this->explodeTypes($data);
//remove 'null' from the list to identify if the signature type should be nullable
if (($nullablePosition = \array_search('null', $returnTypes)) !== false) {
if (($nullablePosition = \array_search('null', $returnTypes, true)) !== false) {
$nullable = true;
\array_splice($returnTypes, (int) $nullablePosition, 1);
}
//remove 'false' from the list to identify if the function return false on error
if (($falsablePosition = \array_search('false', $returnTypes)) !== false) {
if (($falsablePosition = \array_search('false', $returnTypes, true)) !== false) {
$falsable = true;
\array_splice($returnTypes, (int) $falsablePosition, 1);
}
Expand Down
3 changes: 2 additions & 1 deletion generator/src/ScanObjectsCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Safe;

Expand All @@ -17,7 +18,7 @@ protected function configure(): void
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$scanner = new Scanner(__DIR__ . '/../doc/doc-en/en/reference/');

Expand Down
4 changes: 3 additions & 1 deletion generator/src/Scanner.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

use function array_merge;
Expand All @@ -13,7 +15,7 @@ class Scanner
/**
* @var string[]
*/
private $ignoredFunctions;
private ?array $ignoredFunctions = null;

/**
* @var string[]
Expand Down
2 changes: 2 additions & 0 deletions generator/src/ScannerResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

class ScannerResponse
Expand Down
4 changes: 1 addition & 3 deletions generator/src/Type.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

declare(strict_types=1);

namespace Safe;

class Type
{
/**
* Returns true if the type passed in parameter is a class, false if it is scalar or resource
*
* @param string $type
* @return bool
*/
private static function isClass(string $type): bool
{
Expand Down
3 changes: 2 additions & 1 deletion generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Safe;

class WritePhpFunction
Expand Down Expand Up @@ -128,7 +130,6 @@ private function generateExceptionCode(string $moduleName, Method $method) : str

/**
* @param Parameter[] $params
* @return string
*/
private function displayParamsWithType(array $params): string
{
Expand Down
Loading

0 comments on commit b35aab3

Please sign in to comment.