Skip to content
Permalink
Browse files

Fix #1622 - add allow-backwards-incompatible-changes flag to psalter

The flag prevents psalter from adding native return types
  • Loading branch information...
iluuu1994 authored and muglug committed May 13, 2019
1 parent f33745a commit 2c36a10ac8860fecfa1aad165786fa2892b1e046
@@ -175,6 +175,11 @@ class Codebase
*/
public $diff_methods = false;
/**
* @var bool
*/
public $allow_backwards_incompatible_changes = true;
/**
* @var int
*/
@@ -721,14 +721,31 @@ private static function addOrUpdateReturnType(
$function_like_analyzer->getMethodId(),
$function
);
$codebase = $project_analyzer->getCodebase();
$is_final = true;
$fqcln = $source->getFQCLN();
if ($fqcln !== null) {
$class_storage = $codebase->classlike_storage_provider->get($fqcln);
assert($function instanceof ClassMethod);
$is_final = $function->isFinal() || $class_storage->final;
}
$allow_native_type = !$docblock_only
&& $codebase->php_major_version >= 7
&& (
$codebase->allow_backwards_incompatible_changes
|| $is_final
);
$manipulator->setReturnType(
!$docblock_only && $project_analyzer->getCodebase()->php_major_version >= 7
$allow_native_type
? $inferred_return_type->toPhpString(
$source->getNamespace(),
$source->getAliasedClassesFlipped(),
$source->getFQCLN(),
$project_analyzer->getCodebase()->php_major_version,
$project_analyzer->getCodebase()->php_minor_version
$codebase->php_major_version,
$codebase->php_minor_version
) : null,
$inferred_return_type->toNamespacedString(
$source->getNamespace(),
@@ -21,6 +21,7 @@
'help', 'debug', 'debug-by-line', 'config:', 'file:', 'root:',
'plugin:', 'issues:', 'php-version:', 'dry-run', 'safe-types',
'find-unused-code', 'threads:', 'codeowner:',
'allow-backwards-incompatible-changes:',
];
// get options from command line
@@ -117,6 +118,9 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
--codeowner=[codeowner]
You can specify a GitHub code ownership group, and only that owner's code will be updated.
--allow-backwards-incompatible-changes=BOOL
Allow Psalm modify method signatures that could break code outside the project. Defaults to true.
HELP;
exit;
@@ -302,6 +306,20 @@ function (string $line) : bool {
}
}
if (isset($options['allow-backwards-incompatible-changes'])) {
$allow_backwards_incompatible_changes = filter_var(
$options['allow-backwards-incompatible-changes'],
FILTER_VALIDATE_BOOLEAN,
['flags' => FILTER_NULL_ON_FAILURE]
);
if ($allow_backwards_incompatible_changes === null) {
die('--allow-backwards-incompatible-changes expectes a boolean value [true|false|1|0]' . PHP_EOL);
}
$project_analyzer->getCodebase()->allow_backwards_incompatible_changes = $allow_backwards_incompatible_changes;
}
$plugins = [];
if (isset($options['plugin'])) {
@@ -30,10 +30,11 @@ public function setUp()
* @param string $php_version
* @param string[] $issues_to_fix
* @param bool $safe_types
* @param bool $allow_backwards_incompatible_changes
*
* @return void
*/
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix, $safe_types)
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix, $safe_types, bool $allow_backwards_incompatible_changes = true)
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
@@ -77,6 +78,7 @@ public function testValidCode($input_code, $output_code, $php_version, array $is
false,
$safe_types
);
$this->project_analyzer->getCodebase()->allow_backwards_incompatible_changes = $allow_backwards_incompatible_changes;
$this->project_analyzer->getCodebase()->reportUnusedCode();
@@ -1132,6 +1132,82 @@ protected function foo() {}
['InvalidReturnType'],
false,
],
'fixInvalidIntReturnTypeJustInPhpDocWhenDisallowingBackwardsIncompatibleChanges' => [
'<?php
class A {
/**
* @return int
*/
protected function foo() {}
}',
'<?php
class A {
/**
* @return void
*/
protected function foo() {}
}',
'7.3',
['InvalidReturnType'],
false,
false,
],
'fixInvalidIntReturnTypeInFinalMethodWhenDisallowingBackwardsIncompatibleChanges' => [
'<?php
class A {
/**
* @return int
*/
protected final function foo() {}
}',
'<?php
class A {
/**
* @return void
*/
protected final function foo(): void {}
}',
'7.3',
['InvalidReturnType'],
false,
false,
],
'fixInvalidIntReturnTypeInFinalClassWhenDisallowingBackwardsIncompatibleChanges' => [
'<?php
final class A {
/**
* @return int
*/
protected function foo() {}
}',
'<?php
final class A {
/**
* @return void
*/
protected function foo(): void {}
}',
'7.3',
['InvalidReturnType'],
false,
false,
],
'fixInvalidIntReturnTypeInFunctionWhenDisallowingBackwardsIncompatibleChanges' => [
'<?php
/**
* @return int
*/
function foo() {}',
'<?php
/**
* @return void
*/
function foo(): void {}',
'7.3',
['InvalidReturnType'],
false,
false,
],
];
}
}

0 comments on commit 2c36a10

Please sign in to comment.
You can’t perform that action at this time.