Skip to content

Commit

Permalink
Allow resolving directories from config file location (continued) (#1910
Browse files Browse the repository at this point in the history
)

* Extract function getPsalmHelpText() from psalm.php

* Extract initialiseConfig from psalm.php

* Add -c as valid short option for psalter and psalm-refactor

* Use initialiseConfig in psalter, psalm-refactor and psalm-language-server as well as psalm

* Rely on psalm --alter resolving directory from config file in test

* Remove erronous condition for config file path

This code was based on me wrongly thinking that the config file location
was seprated from the argument name with a space instead of an equals
sign

* Use config dir as current dir in psalm and psalm-refactor, as with psalter and psalm-language-server

* Remove redundant duplicated code

* Refactor: move calls to \Psalm\Config::setComposerClassLoader inside initialiseConfig

* PHPCS fix

* Extract function get_path_to_config from command scripts

* Refactor - extract functions from \Psalm\Config::loadFromXML

* Refactor - reduce verbosity of config loading code

* Allow running e2e tests on windows

* Fix testCompactReport on Windows

* convert line endings to make testCompactReport pass on Windows
  • Loading branch information
bdsl authored and muglug committed Jul 7, 2019
1 parent 94f9346 commit ea83068
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 305 deletions.
180 changes: 65 additions & 115 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,26 +563,31 @@ public static function loadFromXMLFile($file_path, $current_dir)
/**
* Creates a new config object from an XML string
*
* @throws ConfigException
*
* @param string $base_dir
* @param string $file_contents
* @param string|null $current_dir Current working directory, if different to $base_dir
*
* @return self
* @psalm-suppress MixedArgument
* @psalm-suppress MixedPropertyFetch
* @psalm-suppress MixedMethodCall
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedOperand
* @psalm-suppress MixedPropertyAssignment
*/
public static function loadFromXML($base_dir, $file_contents, $current_dir = null)
{
if ($current_dir === null) {
$current_dir = $base_dir;
}

$config = new static();
self::validateXmlConfig($file_contents);

return self::fromXmlAndPaths($base_dir, $file_contents, $current_dir);
}


/**
* @throws ConfigException
*/
private static function validateXmlConfig(string $file_contents): void
{
$schema_path = dirname(dirname(__DIR__)) . '/config.xsd';

if (!file_exists($schema_path)) {
Expand Down Expand Up @@ -625,36 +630,60 @@ public static function loadFromXML($base_dir, $file_contents, $current_dir = nul
}
libxml_clear_errors();
}
}

$config_xml = new SimpleXMLElement($file_contents);

if (isset($config_xml['useDocblockTypes'])) {
$attribute_text = (string) $config_xml['useDocblockTypes'];
$config->use_docblock_types = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['useDocblockPropertyTypes'])) {
$attribute_text = (string) $config_xml['useDocblockPropertyTypes'];
$config->use_docblock_property_types = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['throwExceptionOnError'])) {
$attribute_text = (string) $config_xml['throwExceptionOnError'];
$config->throw_exception = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['hideExternalErrors'])) {
$attribute_text = (string) $config_xml['hideExternalErrors'];
$config->hide_external_errors = $attribute_text === 'true' || $attribute_text === '1';
}
/**
* @psalm-suppress MixedMethodCall
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedOperand
* @psalm-suppress MixedPropertyAssignment
* @psalm-suppress MixedArgument
* @psalm-suppress MixedPropertyFetch
*
* @throws ConfigException
*/
private static function fromXmlAndPaths(string $base_dir, string $file_contents, string $current_dir): self
{
$config = new static();

$config_xml = new SimpleXMLElement($file_contents);

if (isset($config_xml['resolveFromConfigFile'])) {
$attribute_text = (string) $config_xml['resolveFromConfigFile'];
$config->resolve_from_config_file = $attribute_text === 'true' || $attribute_text === '1';
$booleanAttributes = [
'useDocblockTypes' => 'use_docblock_types',
'useDocblockPropertyTypes' => 'use_docblock_property_types',
'throwExceptionOnError' => 'throw_exception',
'hideExternalErrors' => 'hide_external_errors',
'resolveFromConfigFile' => 'resolve_from_config_file',
'allowFileIncludes' => 'allow_includes',
'totallyTyped' => 'totally_typed',
'strictBinaryOperands' => 'strict_binary_operands',
'requireVoidReturnType' => 'add_void_docblocks',
'useAssertForType' => 'use_assert_for_type',
'rememberPropertyAssignmentsAfterCall' => 'remember_property_assignments_after_call',
'allowPhpStormGenerics' => 'allow_phpstorm_generics',
'allowStringToStandInForClass' => 'allow_string_standin_for_class',
'usePhpDocMethodsWithoutMagicCall' => 'use_phpdoc_method_without_magic_or_parent',
'memoizeMethodCallResults' => 'memoize_method_calls',
'hoistConstants' => 'hoist_constants',
'addParamDefaultToDocblockType' => 'add_param_default_to_docblock_type',
'checkForThrowsDocblock' => 'check_for_throws_docblock',
'checkForThrowsInGlobalScope' => 'check_for_throws_in_global_scope',
'forbidEcho' => 'forbid_echo',
'ignoreInternalFunctionFalseReturn' => 'ignore_internal_falsable_issues',
'ignoreInternalFunctionNullReturn' => 'ignore_internal_nullable_issues',
];

foreach ($booleanAttributes as $xmlName => $internalName) {
if (isset($config_xml[$xmlName])) {
$attribute_text = (string) $config_xml[$xmlName];
$config->setBooleanAttribute(
$internalName,
$attribute_text === 'true' || $attribute_text === '1'
);
}
}


if ($config->resolve_from_config_file) {
$config->base_dir = $base_dir;
} else {
Expand Down Expand Up @@ -686,87 +715,13 @@ public static function loadFromXML($base_dir, $file_contents, $current_dir = nul
trigger_error('Could not create cache directory: ' . $config->cache_directory, E_USER_ERROR);
}

if (isset($config_xml['allowFileIncludes'])) {
$attribute_text = (string) $config_xml['allowFileIncludes'];
$config->allow_includes = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['totallyTyped'])) {
$attribute_text = (string) $config_xml['totallyTyped'];
$config->totally_typed = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['strictBinaryOperands'])) {
$attribute_text = (string) $config_xml['strictBinaryOperands'];
$config->strict_binary_operands = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['requireVoidReturnType'])) {
$attribute_text = (string) $config_xml['requireVoidReturnType'];
$config->add_void_docblocks = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['useAssertForType'])) {
$attribute_text = (string) $config_xml['useAssertForType'];
$config->use_assert_for_type = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['rememberPropertyAssignmentsAfterCall'])) {
$attribute_text = (string) $config_xml['rememberPropertyAssignmentsAfterCall'];
$config->remember_property_assignments_after_call = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['serializer'])) {
$attribute_text = (string) $config_xml['serializer'];
$config->use_igbinary = $attribute_text === 'igbinary';
} elseif ($igbinary_version = phpversion('igbinary')) {
$config->use_igbinary = version_compare($igbinary_version, '2.0.5') >= 0;
}

if (isset($config_xml['allowPhpStormGenerics'])) {
$attribute_text = (string) $config_xml['allowPhpStormGenerics'];
$config->allow_phpstorm_generics = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['allowStringToStandInForClass'])) {
$attribute_text = (string) $config_xml['allowStringToStandInForClass'];
$config->allow_string_standin_for_class = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['usePhpDocMethodsWithoutMagicCall'])) {
$attribute_text = (string) $config_xml['usePhpDocMethodsWithoutMagicCall'];
$config->use_phpdoc_method_without_magic_or_parent = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['memoizeMethodCallResults'])) {
$attribute_text = (string) $config_xml['memoizeMethodCallResults'];
$config->memoize_method_calls = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['hoistConstants'])) {
$attribute_text = (string) $config_xml['hoistConstants'];
$config->hoist_constants = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['addParamDefaultToDocblockType'])) {
$attribute_text = (string) $config_xml['addParamDefaultToDocblockType'];
$config->add_param_default_to_docblock_type = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['checkForThrowsDocblock'])) {
$attribute_text = (string) $config_xml['checkForThrowsDocblock'];
$config->check_for_throws_docblock = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['checkForThrowsInGlobalScope'])) {
$attribute_text = (string) $config_xml['checkForThrowsInGlobalScope'];
$config->check_for_throws_in_global_scope = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['forbidEcho'])) {
$attribute_text = (string) $config_xml['forbidEcho'];
$config->forbid_echo = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['findUnusedCode'])) {
$attribute_text = (string) $config_xml['findUnusedCode'];
Expand All @@ -779,16 +734,6 @@ public static function loadFromXML($base_dir, $file_contents, $current_dir = nul
$config->find_unused_variables = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['ignoreInternalFunctionFalseReturn'])) {
$attribute_text = (string) $config_xml['ignoreInternalFunctionFalseReturn'];
$config->ignore_internal_falsable_issues = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['ignoreInternalFunctionNullReturn'])) {
$attribute_text = (string) $config_xml['ignoreInternalFunctionNullReturn'];
$config->ignore_internal_nullable_issues = $attribute_text === 'true' || $attribute_text === '1';
}

if (isset($config_xml['errorBaseline'])) {
$attribute_text = (string) $config_xml['errorBaseline'];
$config->error_baseline = $attribute_text;
Expand Down Expand Up @@ -1811,4 +1756,9 @@ public function addStubFile(string $stub_file)
{
$this->stub_files[] = $stub_file;
}

private function setBooleanAttribute(string $name, bool $value): void
{
$this->$name = $value;
}
}
Loading

0 comments on commit ea83068

Please sign in to comment.