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

Fixed ignoreTypeStats & useStrictTypes flag value detection for directories in config #7182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/Psalm/Config/FileFilter.php
Expand Up @@ -111,12 +111,8 @@ public static function loadFromArray(
/** @var array $directory */
foreach ($config['directory'] as $directory) {
$directory_path = (string) ($directory['name'] ?? '');
$ignore_type_stats = strtolower(
isset($directory['ignoreTypeStats']) ? (string) $directory['ignoreTypeStats'] : ''
) === 'true';
$declare_strict_types = strtolower(
isset($directory['useStrictTypes']) ? (string) $directory['useStrictTypes'] : ''
) === 'true';
$ignore_type_stats = (bool) ($directory['ignoreTypeStats'] ?? false);
$declare_strict_types = (bool) ($directory['useStrictTypes'] ?? false);

if ($directory_path[0] === '/' && DIRECTORY_SEPARATOR === '/') {
$prospective_directory_path = $directory_path;
Expand Down Expand Up @@ -350,13 +346,8 @@ public static function loadFromXMLElement(
foreach ($e->directory as $directory) {
$config['directory'][] = [
'name' => (string) $directory['name'],
'ignoreTypeStats' => strtolower(
isset($directory['ignoreTypeStats']) ? (string) $directory['ignoreTypeStats'] : ''
) === 'true',

'useStrictTypes' => strtolower(
isset($directory['useStrictTypes']) ? (string) $directory['useStrictTypes'] : ''
) === 'true',
'ignoreTypeStats' => strtolower((string) ($directory['ignoreTypeStats'] ?? '')) === 'true',
'useStrictTypes' => strtolower((string) ($directory['useStrictTypes'] ?? '')) === 'true',
];
}
}
Expand Down
66 changes: 66 additions & 0 deletions tests/Config/ConfigTest.php
Expand Up @@ -1483,4 +1483,70 @@ public function pluginRegistersScannerAndAnalyzer(int $flags, ?int $expectedExce
self::assertSame(get_class($analyzerMock), $config->getFiletypeAnalyzers()[$extension] ?? null);
self::assertNull($expectedExceptionCode, 'Expected exception code was not thrown');
}

public function testTypeStatsForFileReporting(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are failing on windows. I'm pretty sure this has to do with path separators in getcwd()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've no idea how to approach this issue. I don't have access to windows machine.
testBarebonesConfig() also uses getcwd(). I wonder how it is working.

Should I try using dirname(__DIR__, 2) instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's not getcwd(), the issue is with . '/' at the end of paths. You can replace with . DIRECTORY_SEPARATOR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, it worked. Thanks.
I should be careful with dir separator from next time.

{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string) getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory ignoreTypeStats="true" name="src/Psalm/Config" />
<directory ignoreTypeStats="1" name="src/Psalm/Internal" />
<directory ignoreTypeStats="true1" name="src/Psalm/Issue" />
<directory ignoreTypeStats="false" name="src/Psalm/Node" />
<directory ignoreTypeStats="invalid" name="src/Psalm/Plugin" />
<directory ignoreTypeStats="0" name="src/Psalm/Progress" />
<directory ignoreTypeStats="" name="src/Psalm/Report" />
<directory name="src/Psalm/SourceControl" />
</projectFiles>
</psalm>'
)
);

$config = $this->project_analyzer->getConfig();

$this->assertFalse($config->reportTypeStatsForFile(realpath('src/Psalm/Config') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Internal') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Issue') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Node') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Plugin') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Progress') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/Report') . DIRECTORY_SEPARATOR));
$this->assertTrue($config->reportTypeStatsForFile(realpath('src/Psalm/SourceControl') . DIRECTORY_SEPARATOR));
}

public function testStrictTypesForFileReporting(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Config::loadFromXML(
(string) getcwd(),
'<?xml version="1.0"?>
<psalm>
<projectFiles>
<directory useStrictTypes="true" name="src/Psalm/Config" />
<directory useStrictTypes="1" name="src/Psalm/Internal" />
<directory useStrictTypes="true1" name="src/Psalm/Issue" />
<directory useStrictTypes="false" name="src/Psalm/Node" />
<directory useStrictTypes="invalid" name="src/Psalm/Plugin" />
<directory useStrictTypes="0" name="src/Psalm/Progress" />
<directory useStrictTypes="" name="src/Psalm/Report" />
<directory name="src/Psalm/SourceControl" />
</projectFiles>
</psalm>'
)
);

$config = $this->project_analyzer->getConfig();

$this->assertTrue($config->useStrictTypesForFile(realpath('src/Psalm/Config') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Internal') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Issue') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Node') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Plugin') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Progress') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/Report') . DIRECTORY_SEPARATOR));
$this->assertFalse($config->useStrictTypesForFile(realpath('src/Psalm/SourceControl') . DIRECTORY_SEPARATOR));
}
}