rollback Required attribute and use PhpStormStubsElementAvailable instead

This commit is contained in:
Ivan Fedorov 2021-07-27 21:19:22 +02:00 committed by Ivan Fedorov
parent 9703f8569a
commit fc05cca170
14 changed files with 73 additions and 104 deletions

View File

@ -2496,7 +2496,8 @@ function curl_reset(#[LanguageLevelTypeAware(['8.0' => 'CurlHandle'], default: '
*/
function curl_multi_exec(
#[LanguageLevelTypeAware(['8.0' => 'CurlMultiHandle'], default: 'resource')] $multi_handle,
#[Required(from: '8.0')] &$still_running = false
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] &$still_running = 0,
#[PhpStormStubsElementAvailable(from: '8.0')] &$still_running
): int {}
/**

View File

@ -5,7 +5,6 @@ use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
use JetBrains\PhpStorm\Internal\PhpStormStubsElementAvailable;
use JetBrains\PhpStorm\Internal\Required;
use JetBrains\PhpStorm\Pure;
/**
@ -462,7 +461,8 @@ function gmdate(string $format, ?int $timestamp) {}
*/
#[Pure]
function mktime(
#[Required(from: '8.0')] int $hour = null,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] int $hour = null,
#[PhpStormStubsElementAvailable(from: '8.0')] int $hour,
?int $minute = null,
?int $second = null,
?int $month = null,
@ -501,7 +501,8 @@ function mktime(
*/
#[Pure]
function gmmktime(
#[Required(from: '8.0')] int $hour = null,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] int $hour = null,
#[PhpStormStubsElementAvailable(from: '8.0')] int $hour,
?int $minute = null,
?int $second = null,
?int $month = null,

View File

@ -494,7 +494,7 @@ class DOMImplementation
* @throws DOMException If there is an error with the namespace
*/
public function createDocumentType(
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $qualifiedName ,
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $qualifiedName,
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $publicId = '',
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $systemId = ''
) {}
@ -1520,7 +1520,8 @@ class DOMAttr extends DOMNode
public function __construct(
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $name,
#[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $value
= '') {}
= ''
) {}
}
/**

View File

@ -5,7 +5,6 @@
use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
use JetBrains\PhpStorm\Internal\PhpStormStubsElementAvailable;
use JetBrains\PhpStorm\Internal\Required;
use JetBrains\PhpStorm\Pure;
/**
@ -816,9 +815,8 @@ function mb_decode_mimeheader(string $string): string {}
function mb_convert_variables(
string $to_encoding,
array|string $from_encoding,
#[PhpStormStubsElementAvailable(from: '8.0')]
mixed &$var,
#[Required(from: '5.3', to: '7.4')]
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] &$vars,
#[PhpStormStubsElementAvailable(from: '8.0')] mixed &$var,
mixed &...$vars
): string|false {}

View File

@ -1,21 +0,0 @@
<?php
namespace JetBrains\PhpStorm\Internal;
use Attribute;
use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\ExpectedValues;
/**
* For PhpStorm internal use only
* @since 8.0
* @internal
*/
#[Attribute(Attribute::TARGET_PARAMETER)]
class Required
{
public function __construct(
#[ExpectedValues(Deprecated::PHP_VERSIONS)] $from,
#[ExpectedValues(Deprecated::PHP_VERSIONS)] $to = null
) {}
}

View File

@ -778,7 +778,15 @@ function openssl_verify(
* <i>sealed_data</i>, and the envelope keys in
* <i>env_keys</i>.
*/
function openssl_seal(string $data, &$sealed_data, &$encrypted_keys, array $public_key, #[Required(from: '8.0')] string $cipher_algo = "RC4", &$iv = ''): int|false {}
function openssl_seal(
string $data,
&$sealed_data,
&$encrypted_keys,
array $public_key,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] string $cipher_algo = '',
#[PhpStormStubsElementAvailable(from: '8.0')] string $cipher_algo,
&$iv = ''
): int|false {}
/**
* Open sealed data
@ -799,7 +807,8 @@ function openssl_open(
&$output,
string $encrypted_key,
#[LanguageLevelTypeAware(['8.0' => 'OpenSSLAsymmetricKey|OpenSSLCertificate|array|string'], default: 'resource|array|string')] $private_key,
#[Required(from: '8.0')] string $cipher_algo = "RC4",
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] string $cipher_algo = '',
#[PhpStormStubsElementAvailable(from: '8.0')] string $cipher_algo,
?string $iv
): bool {}

View File

@ -4,7 +4,6 @@ use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
use JetBrains\PhpStorm\Internal\PhpStormStubsElementAvailable;
use JetBrains\PhpStorm\Internal\Required;
use JetBrains\PhpStorm\Pure;
/**
@ -1097,10 +1096,8 @@ function join(array|string $separator = "", ?array $array): string {}
function setlocale(
int $category,
#[PhpStormStubsElementAvailable(from: '8.0')] $locales,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')]
#[Required(from: '5.3', to: '7.4')]
...$locale,
#[PhpStormStubsElementAvailable(from: '8.0')] ...$rest
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $rest,
...$rest
): string|false {}
/**

View File

@ -2,6 +2,7 @@
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
use JetBrains\PhpStorm\Internal\PhpStormStubsElementAvailable;
use JetBrains\PhpStorm\Internal\Required;
use JetBrains\PhpStorm\Internal\ReturnTypeContract as TypeContract;
use JetBrains\PhpStorm\Pure;
@ -230,7 +231,11 @@ function ord(string $character): int {}
* </p>
* @return void
*/
function parse_str(string $string, #[Required(from: '8.0')] &$result = []): void {}
function parse_str(
string $string,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] &$result = [],
#[PhpStormStubsElementAvailable(from: '8.0')] &$result
): void {}
/**
* Parse a CSV string into an array

View File

@ -215,7 +215,11 @@ function var_export(mixed $value, bool $return = false): ?string {}
* </p>
* @return void
*/
function debug_zval_dump(#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value, #[Required(from: '5.3', to: '7.4')] mixed ...$values): void {}
function debug_zval_dump(
#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $values,
mixed ...$values
): void {}
/**
* Prints human-readable information about a variable
@ -362,7 +366,10 @@ function highlight_string(string $string, bool $return = false): string|bool {}
* Otherwise the nanoseconds are returned as integer (64bit platforms) or float (32bit platforms).
*/
#[Pure]
function hrtime(#[Required(from: '5.3', to: '7.4')] bool $as_number = false): array|int|float|false {}
function hrtime(
#[PhpStormStubsElementAvailable(from: '7.3', to: '7.4')] bool $as_number,
#[PhpStormStubsElementAvailable(from: '8.0')] bool $as_number = false
): array|int|float|false {}
/**
* Return source with stripped comments and whitespace

View File

@ -3,7 +3,6 @@
use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Internal\LanguageLevelTypeAware;
use JetBrains\PhpStorm\Internal\PhpStormStubsElementAvailable;
use JetBrains\PhpStorm\Internal\Required;
use JetBrains\PhpStorm\Pure;
/**
@ -169,7 +168,11 @@ function pfsockopen(string $hostname, int $port = -1, &$error_code, &$error_mess
*/
#[Pure]
#[LanguageLevelTypeAware(["8.0" => "string"], default: "string|false")]
function pack(string $format, #[Required(from: '5.3', to: '7.3')] mixed ...$values) {}
function pack(
string $format,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.3')] $values,
mixed ...$values
) {}
/**
* Unpack data from binary string

View File

@ -732,7 +732,11 @@ function key(object|array $array): string|int|null {}
* parameter values.
*/
#[Pure]
function min(#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value, #[Required(from: '5.3', to: '7.4')] mixed ...$values): mixed {}
function min(
#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] mixed $values,
mixed ...$values
): mixed {}
/**
* Find highest value
@ -743,7 +747,11 @@ function min(#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value, #[Requi
* parameter values, either within a arg array or two arguments.
*/
#[Pure]
function max(#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value, #[Required(from: '5.3', to: '7.4')] mixed ...$values): mixed {}
function max(
#[PhpStormStubsElementAvailable(from: '8.0')] mixed $value,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] mixed $values,
mixed ...$values
): mixed {}
/**
* Checks if a value exists in an array
@ -843,7 +851,7 @@ function extract(array &$array, int $flags, string $prefix): int {}
* @return array the output array with all the variables added to it.
*/
#[Pure]
function compact(#[PhpStormStubsElementAvailable(from: '8.0')] $var_name, #[Required(from: '5.3', to: '7.4')] ...$var_names): array {}
function compact(#[PhpStormStubsElementAvailable(from: '8.0')] $var_name, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $var_names, ...$var_names): array {}
/**
* Fill an array with values
@ -932,7 +940,11 @@ function array_multisort(
* </p>
* @return int the number of elements in the array.
*/
function array_push(array &$array, #[Required(from: '5.3', to: '7.2')] mixed ...$values): int {}
function array_push(
array &$array,
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.2')] $values,
mixed ...$values
): int {}
/**
* Pop the element off the end of array
@ -972,7 +984,7 @@ function array_shift(array &$array): mixed {}
* </p>
* @return int the number of elements in the array.
*/
function array_unshift(array &$array, #[Required(from: '5.3', to: '7.2')] mixed ...$values): int {}
function array_unshift(array &$array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.2')] $values, mixed ...$values): int {}
/**
* Remove a portion of the array and replace it with something else

View File

@ -272,7 +272,7 @@ function array_unique(array $array, int $flags = SORT_STRING): array {}
* @meta
*/
#[Pure]
function array_intersect(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_intersect(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the intersection of arrays using keys for comparison
@ -287,7 +287,7 @@ function array_intersect(array $array, #[Required(from: '5.3', to: '7.4')] array
* @meta
*/
#[Pure]
function array_intersect_key(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_intersect_key(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the intersection of arrays using a callback function on the keys for comparison
@ -355,7 +355,7 @@ function array_uintersect(
* @meta
*/
#[Pure]
function array_intersect_assoc(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_intersect_assoc(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the intersection of arrays with additional index check, compares data by a callback function
@ -454,7 +454,7 @@ function array_uintersect_uassoc(
* @meta
*/
#[Pure]
function array_diff(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_diff(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the difference of arrays using keys for comparison
@ -471,7 +471,7 @@ function array_diff(array $array, #[Required(from: '5.3', to: '7.4')] array ...$
* @meta
*/
#[Pure]
function array_diff_key(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_diff_key(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the difference of arrays using a callback function on the keys for comparison
@ -544,7 +544,7 @@ function array_udiff(
* @meta
*/
#[Pure]
function array_diff_assoc(array $array, #[Required(from: '5.3', to: '7.4')] array ...$arrays): array {}
function array_diff_assoc(array $array, #[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays, array ...$arrays): array {}
/**
* Computes the difference of arrays with additional index check, compares data by a callback function
@ -730,7 +730,8 @@ function array_filter(array $array, ?callable $callback, int $mode = 0): array {
function array_map(
?callable $callback,
#[PhpStormStubsElementAvailable(from: '8.0')] array $array,
#[Required(from: '5.3', to: '7.4')] array ...$arrays
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')] $arrays,
array ...$arrays
): array {}
/**

View File

@ -56,9 +56,11 @@ class BaseFunctionsTest extends BaseStubsTest
$phpstormFunction->parameters,
fn ($parameter) => BasePHPElement::entitySuitesCurrentPhpVersion($parameter)
);
$uniqueParameterNames = array_unique(array_map(fn (PHPParameter $parameter) => $parameter->name, $filteredStubParameters));
static::assertSameSize(
$function->parameters,
$filteredStubParameters,
$uniqueParameterNames,
"Parameter number mismatch for function $functionName.
Expected: " . BaseStubsTest::getParameterRepresentation($function) . "\n" .
'Actual: ' . BaseStubsTest::getParameterRepresentation($phpstormFunction)

View File

@ -3,11 +3,7 @@ declare(strict_types=1);
namespace StubTests\Model;
use JetBrains\PhpStorm\Internal\Required;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use ReflectionParameter;
use stdClass;
@ -23,7 +19,6 @@ class PHPParameter extends BasePHPElement
public $is_vararg = false;
public $is_passed_by_ref = false;
public $isOptional = false;
public $requiredInVersionsRange = [];
public $defaultValue = null;
/**
@ -61,15 +56,7 @@ class PHPParameter extends BasePHPElement
$this->is_vararg = $node->variadic;
$this->is_passed_by_ref = $node->byRef;
$this->defaultValue = $node->default;
$this->requiredInVersionsRange = self::findRequiredVersionsRangeFromAttribute($node->attrGroups);
if (!empty($this->requiredInVersionsRange)) {
$requiredFromAttribute = (doubleval(getenv('PHP_VERSION')) >= $this->requiredInVersionsRange['from'] &&
doubleval(getenv('PHP_VERSION')) <= $this->requiredInVersionsRange['to']);
$this->isOptional = ($this->is_vararg || !empty($this->defaultValue)) && !$requiredFromAttribute;
} else {
$this->isOptional = ($this->is_vararg || !empty($this->defaultValue));
}
$this->isOptional = !empty($this->defaultValue) || $this->is_vararg;
return $this;
}
@ -118,38 +105,4 @@ class PHPParameter extends BasePHPElement
}
}
}
/**
* @param AttributeGroup[] $attrGroups
* @return array
*/
private static function findRequiredVersionsRangeFromAttribute(array $attrGroups): array
{
$versionRange = [];
foreach ($attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === Required::class) {
if (count($attr->args) == 2) {
foreach ($attr->args as $arg) {
$versionRange[$arg->name->name] = (float)$arg->value->value;
}
} else {
$arg = $attr->args[0]->value;
if ($arg instanceof Array_) {
$value = $arg->items[0]->value;
if ($value instanceof String_) {
return ['from' => (float)$value->value];
}
} else {
$rangeName = $attr->args[0]->name;
return $rangeName === null || $rangeName->name == 'from' ?
['from' => (float)$arg->value, 'to' => PhpVersions::getLatest()] :
['from' => PhpVersions::getFirst(), 'to' => (float)$arg->value];
}
}
}
}
}
return $versionRange;
}
}