Skip to content

Commit

Permalink
Merge remote-tracking branch 'vimeo/5.x' into merge_5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 15, 2024
2 parents 4dd06f7 + b958349 commit 3f82312
Show file tree
Hide file tree
Showing 52 changed files with 2,859 additions and 251 deletions.
1 change: 1 addition & 0 deletions config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@
<xs:element name="RedundantCastGivenDocblockType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantCondition" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantConditionGivenDocblockType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantFlag" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantFunctionCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantFunctionCallGivenDocblockType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="RedundantIdentityWithTrue" type="IssueHandlerType" minOccurs="0" />
Expand Down
20 changes: 10 additions & 10 deletions dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2847,11 +2847,11 @@
'FilesystemIterator::setInfoClass' => ['void', 'class='=>'class-string'],
'FilesystemIterator::valid' => ['bool'],
'filetype' => ['string|false', 'filename'=>'string'],
'filter_has_var' => ['bool', 'input_type'=>'int', 'var_name'=>'string'],
'filter_has_var' => ['bool', 'input_type'=>'0|1|2|4|5', 'var_name'=>'string'],
'filter_id' => ['int|false', 'name'=>'string'],
'filter_input' => ['mixed|false', 'type'=>'int', 'var_name'=>'string', 'filter='=>'int', 'options='=>'array|int'],
'filter_input_array' => ['array|false|null', 'type'=>'int', 'options='=>'int|array', 'add_empty='=>'bool'],
'filter_list' => ['array'],
'filter_input' => ['mixed|false|null', 'type'=>'0|1|2|4|5', 'var_name'=>'string', 'filter='=>'int', 'options='=>'array|int'],
'filter_input_array' => ['array|false|null', 'type'=>'0|1|2|4|5', 'options='=>'int|array', 'add_empty='=>'bool'],
'filter_list' => ['non-empty-list<non-falsy-string>'],
'filter_var' => ['mixed|false', 'value'=>'mixed', 'filter='=>'int', 'options='=>'array|int'],
'filter_var_array' => ['array|false|null', 'array'=>'array', 'options='=>'array|int', 'add_empty='=>'bool'],
'FilterIterator::__construct' => ['void', 'iterator'=>'Iterator'],
Expand Down Expand Up @@ -3315,7 +3315,7 @@
'getopt' => ['array<string,string|false|list<string|false>>|false', 'short_options'=>'string', 'long_options='=>'array', '&w_rest_index='=>'int'],
'getprotobyname' => ['int|false', 'protocol'=>'string'],
'getprotobynumber' => ['string', 'protocol'=>'int'],
'getrandmax' => ['int'],
'getrandmax' => ['int<1, max>'],
'getrusage' => ['array', 'mode='=>'int'],
'getservbyname' => ['int|false', 'service'=>'string', 'protocol'=>'string'],
'getservbyport' => ['string|false', 'port'=>'int', 'protocol'=>'string'],
Expand Down Expand Up @@ -6625,7 +6625,7 @@
'mb_ereg_search_setpos' => ['bool', 'offset'=>'int'],
'mb_eregi' => ['bool', 'pattern'=>'string', 'string'=>'string', '&w_matches='=>'array|null'],
'mb_eregi_replace' => ['string|false|null', 'pattern'=>'string', 'replacement'=>'string', 'string'=>'string', 'options='=>'string|null'],
'mb_get_info' => ['array|string|int|false', 'type='=>'string'],
'mb_get_info' => ['array|string|int|false|null', 'type='=>'string'],
'mb_http_input' => ['array|string|false', 'type='=>'string|null'],
'mb_http_output' => ['string|bool', 'encoding='=>'string|null'],
'mb_internal_encoding' => ['string|bool', 'encoding='=>'string|null'],
Expand Down Expand Up @@ -7646,7 +7646,7 @@
'msql_query' => ['resource', 'query'=>'string', 'link_identifier='=>'?resource'],
'msql_result' => ['string', 'result'=>'resource', 'row'=>'int', 'field='=>'mixed'],
'msql_select_db' => ['bool', 'database_name'=>'string', 'link_identifier='=>'?resource'],
'mt_getrandmax' => ['int'],
'mt_getrandmax' => ['int<1, max>'],
'mt_rand' => ['int', 'min'=>'int', 'max'=>'int'],
'mt_rand\'1' => ['int'],
'mt_srand' => ['void', 'seed='=>'?int', 'mode='=>'int'],
Expand Down Expand Up @@ -11141,8 +11141,8 @@
'SessionIdInterface::create_sid' => ['string'],
'SessionUpdateTimestampHandler::updateTimestamp' => ['bool', 'id'=>'string', 'data'=>'string'],
'SessionUpdateTimestampHandler::validateId' => ['char', 'id'=>'string'],
'SessionUpdateTimestampHandlerInterface::updateTimestamp' => ['bool', 'key'=>'string', 'value'=>'string'],
'SessionUpdateTimestampHandlerInterface::validateId' => ['bool', 'key'=>'string'],
'SessionUpdateTimestampHandlerInterface::updateTimestamp' => ['bool', 'id'=>'string', 'data'=>'string'],
'SessionUpdateTimestampHandlerInterface::validateId' => ['bool', 'id'=>'string'],
'set_error_handler' => ['null|callable(int,string,string=,int=,array=):bool', 'callback'=>'null|callable(int,string,string=,int=,array=):bool', 'error_levels='=>'int'],
'set_exception_handler' => ['null|callable(Throwable):void', 'callback'=>'null|callable(Throwable):void'],
'set_file_buffer' => ['int', 'stream'=>'resource', 'size'=>'int'],
Expand Down Expand Up @@ -14165,7 +14165,7 @@
'unlink' => ['bool', 'filename'=>'string', 'context='=>'resource'],
'unpack' => ['array|false', 'format'=>'string', 'string'=>'string', 'offset='=>'int'],
'unregister_tick_function' => ['void', 'callback'=>'callable'],
'unserialize' => ['mixed', 'data'=>'string', 'options='=>'array{allowed_classes?:string[]|bool}'],
'unserialize' => ['mixed', 'data'=>'string', 'options='=>'array{allowed_classes?:class-string[]|bool}'],
'unset' => ['void', 'var='=>'mixed', '...args='=>'mixed'],
'untaint' => ['bool', '&rw_string'=>'string', '&...rw_strings='=>'string'],
'uopz_allow_exit' => ['void', 'allow'=>'bool'],
Expand Down
4 changes: 4 additions & 0 deletions dictionaries/CallMap_82_delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
'old' => ['non-empty-list<string>', 'string'=>'string', 'length='=>'positive-int'],
'new' => ['list<non-empty-string>', 'string'=>'string', 'length='=>'positive-int'],
],
'mb_get_info' => [
'old' => ['array|string|int|false', 'type='=>'string'],
'new' => ['array|string|int|false|null', 'type='=>'string'],
],
],

'removed' => [
Expand Down
18 changes: 9 additions & 9 deletions dictionaries/CallMap_historical.php
Original file line number Diff line number Diff line change
Expand Up @@ -6583,8 +6583,8 @@
'SessionIdInterface::create_sid' => ['string'],
'SessionUpdateTimestampHandler::updateTimestamp' => ['bool', 'id'=>'string', 'data'=>'string'],
'SessionUpdateTimestampHandler::validateId' => ['char', 'id'=>'string'],
'SessionUpdateTimestampHandlerInterface::updateTimestamp' => ['bool', 'key'=>'string', 'value'=>'string'],
'SessionUpdateTimestampHandlerInterface::validateId' => ['bool', 'key'=>'string'],
'SessionUpdateTimestampHandlerInterface::updateTimestamp' => ['bool', 'id'=>'string', 'data'=>'string'],
'SessionUpdateTimestampHandlerInterface::validateId' => ['bool', 'id'=>'string'],
'SimpleXMLElement::__construct' => ['void', 'data'=>'string', 'options='=>'int', 'dataIsURL='=>'bool', 'namespaceOrPrefix='=>'string', 'isPrefix='=>'bool'],
'SimpleXMLElement::__get' => ['SimpleXMLElement', 'name'=>'string'],
'SimpleXMLElement::__toString' => ['string'],
Expand Down Expand Up @@ -10434,11 +10434,11 @@
'filepro_rowcount' => ['int'],
'filesize' => ['int|false', 'filename'=>'string'],
'filetype' => ['string|false', 'filename'=>'string'],
'filter_has_var' => ['bool', 'input_type'=>'int', 'var_name'=>'string'],
'filter_has_var' => ['bool', 'input_type'=>'0|1|2|4|5', 'var_name'=>'string'],
'filter_id' => ['int|false', 'name'=>'string'],
'filter_input' => ['mixed|false', 'type'=>'int', 'var_name'=>'string', 'filter='=>'int', 'options='=>'array|int'],
'filter_input_array' => ['array|false|null', 'type'=>'int', 'options='=>'int|array', 'add_empty='=>'bool'],
'filter_list' => ['array'],
'filter_input' => ['mixed|false|null', 'type'=>'0|1|2|4|5', 'var_name'=>'string', 'filter='=>'int', 'options='=>'array|int'],
'filter_input_array' => ['array|false|null', 'type'=>'0|1|2|4|5', 'options='=>'int|array', 'add_empty='=>'bool'],
'filter_list' => ['non-empty-list<non-falsy-string>'],
'filter_var' => ['mixed|false', 'value'=>'mixed', 'filter='=>'int', 'options='=>'array|int'],
'filter_var_array' => ['array|false|null', 'array'=>'array', 'options='=>'array|int', 'add_empty='=>'bool'],
'finfo::__construct' => ['void', 'flags='=>'int', 'magic_database='=>'string'],
Expand Down Expand Up @@ -10679,7 +10679,7 @@
'getopt' => ['array<string,string|false|list<string|false>>|false', 'short_options'=>'string', 'long_options='=>'array'],
'getprotobyname' => ['int|false', 'protocol'=>'string'],
'getprotobynumber' => ['string', 'protocol'=>'int'],
'getrandmax' => ['int'],
'getrandmax' => ['int<1, max>'],
'getrusage' => ['array', 'mode='=>'int'],
'getservbyname' => ['int|false', 'service'=>'string', 'protocol'=>'string'],
'getservbyport' => ['string|false', 'port'=>'int', 'protocol'=>'string'],
Expand Down Expand Up @@ -12488,7 +12488,7 @@
'msql_query' => ['resource', 'query'=>'string', 'link_identifier='=>'?resource'],
'msql_result' => ['string', 'result'=>'resource', 'row'=>'int', 'field='=>'mixed'],
'msql_select_db' => ['bool', 'database_name'=>'string', 'link_identifier='=>'?resource'],
'mt_getrandmax' => ['int'],
'mt_getrandmax' => ['int<1, max>'],
'mt_rand' => ['int', 'min'=>'int', 'max'=>'int'],
'mt_rand\'1' => ['int'],
'mt_srand' => ['void', 'seed='=>'int', 'mode='=>'int'],
Expand Down Expand Up @@ -15197,7 +15197,7 @@
'unlink' => ['bool', 'filename'=>'string', 'context='=>'resource'],
'unpack' => ['array', 'format'=>'string', 'string'=>'string'],
'unregister_tick_function' => ['void', 'callback'=>'callable'],
'unserialize' => ['mixed', 'data'=>'string', 'options='=>'array{allowed_classes?:string[]|bool}'],
'unserialize' => ['mixed', 'data'=>'string', 'options='=>'array{allowed_classes?:class-string[]|bool}'],
'unset' => ['void', 'var='=>'mixed', '...args='=>'mixed'],
'untaint' => ['bool', '&rw_string'=>'string', '&...rw_strings='=>'string'],
'uopz_allow_exit' => ['void', 'allow'=>'bool'],
Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
- [RedundantCastGivenDocblockType](issues/RedundantCastGivenDocblockType.md)
- [RedundantCondition](issues/RedundantCondition.md)
- [RedundantConditionGivenDocblockType](issues/RedundantConditionGivenDocblockType.md)
- [RedundantFlag](issues/RedundantFlag.md)
- [RedundantFunctionCall](issues/RedundantFunctionCall.md)
- [RedundantFunctionCallGivenDocblockType](issues/RedundantFunctionCallGivenDocblockType.md)
- [RedundantIdentityWithTrue](issues/RedundantIdentityWithTrue.md)
Expand Down
8 changes: 8 additions & 0 deletions docs/running_psalm/issues/RedundantFlag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RedundantFlag

Emitted when a flag is redundant. e.g. FILTER_NULL_ON_FAILURE won't do anything when the default option is specified

```php
<?php
$x = filter_input(INPUT_GET, 'hello', FILTER_VALIDATE_DOMAIN, array('options' => array('default' => 'world.com'), 'flags' => FILTER_NULL_ON_FAILURE));
```
4 changes: 4 additions & 0 deletions src/Psalm/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Psalm;

use Psalm\Storage\UnserializeMemoryUsageSuppressionTrait;

final class Aliases
{
use UnserializeMemoryUsageSuppressionTrait;

/**
* @var array<lowercase-string, string>
*/
Expand Down
39 changes: 39 additions & 0 deletions src/Psalm/CodeLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ class CodeLocation
public const CATCH_VAR = 6;
public const FUNCTION_PHPDOC_METHOD = 7;

private const PROPERTY_KEYS_FOR_UNSERIALIZE = [
'file_path' => 'file_path',
'file_name' => 'file_name',
'raw_line_number' => 'raw_line_number',
"\0" . self::class . "\0" . 'end_line_number' => 'end_line_number',
'raw_file_start' => 'raw_file_start',
'raw_file_end' => 'raw_file_end',
"\0*\0" . 'file_start' => 'file_start',
"\0*\0" . 'file_end' => 'file_end',
"\0*\0" . 'single_line' => 'single_line',
"\0*\0" . 'preview_start' => 'preview_start',
"\0" . self::class . "\0" . 'preview_end' => 'preview_end',
"\0" . self::class . "\0" . 'selection_start' => 'selection_start',
"\0" . self::class . "\0" . 'selection_end' => 'selection_end',
"\0" . self::class . "\0" . 'column_from' => 'column_from',
"\0" . self::class . "\0" . 'column_to' => 'column_to',
"\0" . self::class . "\0" . 'snippet' => 'snippet',
"\0" . self::class . "\0" . 'text' => 'text',
'docblock_start' => 'docblock_start',
"\0" . self::class . "\0" . 'docblock_start_line_number' => 'docblock_start_line_number',
"\0*\0" . 'docblock_line_number' => 'docblock_line_number',
"\0" . self::class . "\0" . 'regex_type' => 'regex_type',
"\0" . self::class . "\0" . 'have_recalculated' => 'have_recalculated',
'previous_location' => 'previous_location',
];

public function __construct(
FileSource $file_source,
PhpParser\Node $stmt,
Expand Down Expand Up @@ -126,6 +152,19 @@ public function __construct(
$this->docblock_line_number = $comment_line;
}

/**
* Suppresses memory usage when unserializing objects.
*
* @see \Psalm\Storage\UnserializeMemoryUsageSuppressionTrait
*/
public function __unserialize(array $properties): void
{
foreach (self::PROPERTY_KEYS_FOR_UNSERIALIZE as $key => $property_name) {
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
$this->$property_name = $properties[$key];
}
}

/**
* @psalm-suppress PossiblyUnusedMethod Part of public API
* @return static
Expand Down
8 changes: 4 additions & 4 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ public function getSignatureInformation(
if (InternalCallMapHandler::inCallMap($function_symbol)) {
$callables = InternalCallMapHandler::getCallablesFromCallMap($function_symbol);

if (!$callables || !$callables[0]->params) {
if (!$callables || !isset($callables[0]->params)) {
return null;
}

Expand Down Expand Up @@ -1527,7 +1527,7 @@ public function getBeginedLiteralPart(string $file_path, Position $position): st
$offset = $position->toOffset($file_contents);

preg_match('/\$?\w+$/', substr($file_contents, 0, $offset), $matches);

return $matches[0] ?? '';
}

Expand Down Expand Up @@ -1653,7 +1653,7 @@ public function getCompletionItemsForClassishThing(
str_replace('$', '', $property_name),
);
}

foreach ($class_storage->pseudo_property_set_types as $property_name => $type) {
$pseudo_property_types[$property_name] = new CompletionItem(
str_replace('$', '', $property_name),
Expand All @@ -1665,7 +1665,7 @@ public function getCompletionItemsForClassishThing(
str_replace('$', '', $property_name),
);
}

$completion_items = [...$completion_items, ...array_values($pseudo_property_types)];
}

Expand Down
16 changes: 13 additions & 3 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public static function loadFromXMLFile(string $file_path, string $current_dir):
{
$file_contents = file_get_contents($file_path);

$base_dir = dirname($file_path) . DIRECTORY_SEPARATOR;
$base_dir = dirname($file_path);

if ($file_contents === false) {
throw new InvalidArgumentException('Cannot open ' . $file_path);
Expand Down Expand Up @@ -1147,7 +1147,17 @@ private static function fromXmlAndPaths(
// any paths passed via CLI should be added to the projectFiles
// as they're getting analyzed like if they are part of the project
// ProjectAnalyzer::getInstance()->check_paths_files is not populated at this point in time
$paths_to_check = CliUtils::getPathsToCheck(null);

$paths_to_check = null;

global $argv;

// Hack for Symfonys own argv resolution.
// @see https://github.com/vimeo/psalm/issues/10465
if (!isset($argv[0]) || basename($argv[0]) !== 'psalm-plugin') {
$paths_to_check = CliUtils::getPathsToCheck(null);
}

if ($paths_to_check !== null) {
$paths_to_add_to_project_files = array();
foreach ($paths_to_check as $path) {
Expand Down Expand Up @@ -1307,7 +1317,7 @@ private static function fromXmlAndPaths(

$path = Path::isAbsolute($plugin_file_name)
? $plugin_file_name
: $config->base_dir . $plugin_file_name;
: $config->base_dir . DIRECTORY_SEPARATOR . $plugin_file_name;

$config->addPluginPath($path);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function analyze(
null,
true,
),
$this->getSuppressedIssues(),
$storage->getSuppressedIssuesForTemplateExtendParams() + $this->getSuppressedIssues(),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,30 @@ public static function analyze(

$has_numeric_and_non_empty = $has_numeric_type && $has_non_empty;

$non_falsy_string = $numeric_type->getBuilder()->addType(new TNonFalsyString())->freeze();
$left_non_falsy = UnionTypeComparator::isContainedBy(
$codebase,
$left_type,
$non_falsy_string,
);

$right_non_falsy = UnionTypeComparator::isContainedBy(
$codebase,
$right_type,
$non_falsy_string,
);

$all_literals = $left_type->allLiterals() && $right_type->allLiterals();

if ($has_non_empty) {
if ($all_literals) {
$result_type = new Union([new TNonEmptyNonspecificLiteralString]);
} elseif ($all_lowercase) {
$result_type = Type::getNonEmptyLowercaseString();
} elseif ($all_non_empty || $has_numeric_and_non_empty || $left_non_falsy || $right_non_falsy) {
$result_type = Type::getNonFalsyString();
} else {
$result_type = $all_non_empty || $has_numeric_and_non_empty ?
Type::getNonFalsyString() : Type::getNonEmptyString();
$result_type = Type::getNonEmptyString();
}
} else {
if ($all_literals) {
Expand Down
Loading

0 comments on commit 3f82312

Please sign in to comment.