Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes phpstan errors, set PHPStan to level 7 #1951

Merged
merged 10 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"symfony/filesystem": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/finder": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/translation": "^4.4.0 || ^5.0.0 || ^6.0.0",
"symfony/validator": "^4.4.0 || ^5.0.0 || ^6.0.0"
"symfony/validator": "^4.4.0 || ^5.0.0 || ^6.0.0",
"ext-xml": "*"
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
},
"require-dev": {
"ext-pdo": "*",
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 @@ -320,14 +320,12 @@ protected function reapplyBehaviors(array $behaviors)
*/
protected function createForeignKeyFromParameters(Table $table, array $fkParameterData): void
{
if (
empty($fkParameterData['localColumn']) ||
empty($fkParameterData['foreignColumn'])
) {
if (empty($fkParameterData['localColumn']) || empty($fkParameterData['foreignColumn'])) {
$tableName = $this->table->getName();

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

dereuromark marked this conversation as resolved.
Show resolved Hide resolved
$fk = new ForeignKey($fkParameterData['name'] ?? null);
$fk->addReference($fkParameterData['localColumn'], $fkParameterData['foreignColumn']);
$table->addForeignKey($fk);
Expand Down
23 changes: 9 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
*/
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 @@ -458,15 +456,12 @@ private function throwSchemaExceptionWithLocation(string $format, ...$args): voi
*/
private function getLocationDescription(): string
{
$location = '';
if ($this->currentXmlFile !== null) {
$location .= sprintf('file %s,', $this->currentXmlFile);
}

$location = ($this->currentXmlFile !== null) ? sprintf('file %s,', $this->currentXmlFile) : '';
$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);

$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
2 changes: 1 addition & 1 deletion src/Propel/Generator/Config/ArrayToPhpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function convert(array $c): string
// set default datasource
if (isset($c['defaultConnection'])) {
$defaultDatasource = $c['defaultConnection'];
} elseif (isset($c['connections']) && is_array($c['connections'])) {
} elseif (is_array($c['connections'])) {
// fallback to the first datasource
$datasourceNames = array_keys($c['connections']);
$defaultDatasource = $datasourceNames[0];
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