Skip to content

Commit

Permalink
Fixes phpstan errors, set PHPStan to level 7 (#1951)
Browse files Browse the repository at this point in the history
* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors

* Fixes phpstan errors
  • Loading branch information
michbeck authored Jan 25, 2023
1 parent 5531e13 commit a5caaae
Show file tree
Hide file tree
Showing 46 changed files with 324 additions and 164 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"require-dev": {
"ext-pdo": "*",
"ext-json": "*",
"ext-xml": "*",
"monolog/monolog": "^1.3 || ^2.3 || ^3.0",
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^9.5.0",
Expand Down
14 changes: 3 additions & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
includes:
- phpstan-baseline.neon
parameters:
level: 6
level: 7
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
treatPhpDocTypesAsCertain: false
paths:
- '%rootDir%/../../../src/'
ignoreErrors:
- '#Call to an undefined method .+Collection::.+Array\(\)#'
-
message: "#^Method .+\\\\ConsoleHelper::.+\\(\\) has no return type specified#"
path: src/Propel/Generator/Command/Helper/ConsoleHelper.php
-
message: "#^Method .+\\\\ConsoleHelper::.+\\(\\) has parameter \\$messages with no type specified#"
path: src/Propel/Generator/Command/Helper/ConsoleHelper.php
-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\TreeBuilder\\:\\:root\\(\\)\\.$#"
path: src/Propel/Common/Config/PropelConfiguration.php
- '#Call to an undefined method object::.+\(\)#'

3 changes: 2 additions & 1 deletion src/Propel/Common/Config/XmlToArrayConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ protected static function simpleXmlToArray(SimpleXMLElement $xml): array
}
} else {
// otherwise, just add the attribute like a child element
if (is_string($child)) {
if (!is_array($child)) {
$child = [];
}

$child[$ak] = self::getConvertedXmlValue($av);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ protected function reapplyBehaviors(array $behaviors)
}

/**
* @psalm-param array{local_column: string, foreign_table: string, foreign_column: string, relation_only?: string} $fkParameterData
* @psalm-param array{name?: string, localColumn: string, foreignTable: string, foreignColumn: string, relationOnly?: string} $fkParameterData
*
* @param \Propel\Generator\Model\Table $table
* @param array $fkParameterData
Expand All @@ -328,6 +328,7 @@ protected function createForeignKeyFromParameters(Table $table, array $fkParamet

throw new SchemaException("Table `$tableName`: Archivable behavior misses foreign key parameters. Please supply `localColumn`, `foreignTable` and `foreignColumn` for every entry");
}

$fk = new ForeignKey($fkParameterData['name'] ?? null);
$fk->addReference($fkParameterData['localColumn'], $fkParameterData['foreignColumn']);
$table->addForeignKey($fk);
Expand Down
10 changes: 4 additions & 6 deletions src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,9 @@ protected static function getRelatedBySuffix(ForeignKey $fk): string

foreach ($fk->getMapping() as $mapping) {
[$localColumn, $foreignValueOrColumn] = $mapping;
$localColumnName = $localColumn->getPhpName();
$localTable = $fk->getTable();
if (!$localColumnName) {
throw new RuntimeException(sprintf('Could not fetch column: %s in table %s.', $localColumnName, $localTable->getName()));
if (!$localColumn) {
throw new RuntimeException(sprintf('Could not resolve column of foreign key `%s` on table `%s`', $fk->getName(), $localTable->getName()));
}

$tableName = $fk->getTableName();
Expand Down Expand Up @@ -973,10 +972,9 @@ protected static function getRefRelatedBySuffix(ForeignKey $fk): string
$relCol = '';
foreach ($fk->getMapping() as $mapping) {
[$localColumn, $foreignValueOrColumn] = $mapping;
$localColumnName = $localColumn->getPhpName();
$localTable = $fk->getTable();
if (!$localColumnName) {
throw new RuntimeException(sprintf('Could not fetch column: %s in table %s.', $localColumnName, $localTable->getName()));
if (!$localColumn) {
throw new RuntimeException(sprintf('Could not resolve column of foreign key `%s` on table `%s`', $fk->getName(), $localTable->getName()));
}

$tableName = $fk->getTableName();
Expand Down
33 changes: 19 additions & 14 deletions src/Propel/Generator/Builder/Util/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class SchemaReader
private $schema;

/**
* @psalm-suppress UndefinedDocblockClass
* @phpstan-ignore-next-line
* @var \XMLParser|resource|null
* @var \XMLParser|resource
*/
private $parser;

Expand Down Expand Up @@ -158,7 +156,7 @@ public function parseFile(string $xmlFile): ?Schema
return null;
}

return $this->parseString(file_get_contents($xmlFile), $xmlFile);
return $this->parseString((string)file_get_contents($xmlFile), $xmlFile);
}

/**
Expand All @@ -184,7 +182,7 @@ public function parseString(string $xmlString, ?string $xmlFile = null): ?Schema
$this->currentXmlFile = $xmlFile;

$parserStash = $this->parser;
/** @psalm-suppress InvalidPropertyAssignmentValue */

$this->parser = xml_parser_create();
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_object($this->parser, $this);
Expand Down Expand Up @@ -247,7 +245,7 @@ public function startElement($parser, string $tagName, array $attributes): void
}

if ($xmlFile[0] !== '/') {
$xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile);
$xmlFile = (string)realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile);
if (!file_exists($xmlFile)) {
throw new SchemaException(sprintf('Unknown include external `%s`', $xmlFile));
}
Expand Down Expand Up @@ -452,21 +450,28 @@ private function throwSchemaExceptionWithLocation(string $format, ...$args): voi
}

/**
* Builds a human readable description of the current location in the parser, i.e. "file schema.xml line 42, column 43"
* Builds a human-readable description of the current location in the parser, i.e. "file schema.xml line 42, column 43"
*
* @return string
*/
private function getLocationDescription(): string
{
$location = '';
if ($this->currentXmlFile !== null) {
$location .= sprintf('file %s,', $this->currentXmlFile);
$location = ($this->currentXmlFile !== null) ? sprintf('file %s,', $this->currentXmlFile) : '';

/**
* @phpstan-ignore-next-line PHPStan is expecting XMLParser only, while resource is valid too.
*/
$currentLineNumber = xml_get_current_line_number($this->parser);
if ($currentLineNumber) {
$location .= sprintf('line %d', $currentLineNumber);
}

$location .= sprintf('line %d', xml_get_current_line_number($this->parser));
$col = xml_get_current_column_number($this->parser);
if ($col) {
$location .= sprintf(', column %d', $col);
/**
* @phpstan-ignore-next-line PHPStan is expecting XMLParser only, while resource is valid too.
*/
$currentColumnNumber = xml_get_current_column_number($this->parser);
if ($currentColumnNumber) {
$location .= sprintf(', column %d', $currentColumnNumber);
}

return $location;
Expand Down
10 changes: 5 additions & 5 deletions src/Propel/Generator/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ protected function createDirectory(string $directory): void
*/
protected function parseConnection(string $connection): array
{
$pos = strpos($connection, '=');
$name = substr($connection, 0, $pos);
$dsn = substr($connection, $pos + 1, strlen($connection));
$length = strpos($connection, '=') ?: null;
$name = substr($connection, 0, $length);
$dsn = substr($connection, $length + 1, strlen($connection));

$pos = strpos($dsn, ':');
$adapter = substr($dsn, 0, $pos);
$length = strpos($dsn, ':') ?: null;
$adapter = substr($dsn, 0, $length);

$extras = [];
foreach (explode(';', $dsn) as $element) {
Expand Down
4 changes: 2 additions & 2 deletions src/Propel/Generator/Command/ConfigConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ protected function createLoadDatabaseDummyScript(string $loaderDir, OutputInterf
*/
protected function getRelativePathToLoaderScript(string $loaderDir, string $outputDir): string
{
$absoluteLoaderDir = realpath($loaderDir);
$absoluteOutputDir = realpath($outputDir);
$absoluteLoaderDir = (string)realpath($loaderDir);
$absoluteOutputDir = (string)realpath($outputDir);
$fs = new Filesystem();

return $fs->makePathRelative($absoluteLoaderDir, $absoluteOutputDir);
Expand Down
10 changes: 5 additions & 5 deletions src/Propel/Generator/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class InitCommand extends AbstractCommand
public function __construct(?string $name = null)
{
parent::__construct($name);
$this->defaultSchemaDir = getcwd();
$this->defaultSchemaDir = (string)getcwd();
$this->defaultPhpDir = $this->detectDefaultPhpDir();
}

Expand Down Expand Up @@ -195,7 +195,7 @@ private function detectDefaultPhpDir(): string
}
}

return getcwd();
return (string)getcwd();
}

/**
Expand Down Expand Up @@ -401,12 +401,12 @@ private function reverseEngineerSchema(OutputInterface $output, array $options):
$input = new ArrayInput($arrInput);
$result = $this->getApplication()->run($input, $output);

if ($result === 0) {
$schema = file_get_contents($outputDir . '/schema.xml');
} else {
if ($result !== 0) {
exit(1);
}

$schema = (string)file_get_contents($outputDir . '/schema.xml');

$this->getApplication()->setAutoExit(true);

return $schema;
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Command/TestPrepareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function buildFixtures(string $fixturesDir, array $connections, InputI
chdir($this->root . '/' . $fixturesDir);

if (is_file('propel.yaml.dist')) {
$content = file_get_contents('propel.yaml.dist');
$content = (string)file_get_contents('propel.yaml.dist');

$content = str_replace('##DATABASE_VENDOR##', $input->getOption('vendor'), $content);
$content = str_replace('##DATABASE_URL##', $input->getOption('dsn'), $content);
Expand Down
12 changes: 9 additions & 3 deletions src/Propel/Generator/Manager/AbstractManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Propel\Generator\Exception\EngineException;
use Propel\Generator\Model\Database;
use Propel\Generator\Model\Schema;
use RuntimeException;
use XSLTProcessor;

/**
Expand Down Expand Up @@ -292,6 +293,7 @@ public function setLoggerClosure(Closure $logger): void
* class.
*
* @throws \Propel\Generator\Exception\EngineException
* @throws \RuntimeException
* @throws \Propel\Generator\Exception\BuildException
*
* @return void
Expand Down Expand Up @@ -326,6 +328,10 @@ protected function loadDataModels(): void
$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xslDom);
$dom = $xsl->transformToDoc($dom);

if ($dom === false) {
throw new RuntimeException('XSLTProcessor transformation to a DOMDocument failed.');
}
}
}

Expand All @@ -340,7 +346,7 @@ protected function loadDataModels(): void

$xmlParser = new SchemaReader($defaultPlatform, $this->dbEncoding);
$xmlParser->setGeneratorConfig($this->getGeneratorConfig());
$schema = $xmlParser->parseString($dom->saveXML(), $dmFilename);
$schema = $xmlParser->parseString((string)$dom->saveXML(), $dmFilename);
$nbTables = $schema->getDatabase(null, false)->countTables();
$totalNbTables += $nbTables;

Expand Down Expand Up @@ -519,8 +525,8 @@ protected function getProperties(string $file): array
continue;
}

$pos = strpos($line, '=');
$properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
$length = strpos($line, '=') ?: null;
$properties[trim(substr($line, 0, $length))] = trim(substr($line, $length + 1));
}

return $properties;
Expand Down
Loading

0 comments on commit a5caaae

Please sign in to comment.