Skip to content

Commit

Permalink
various cleanup and small refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
isfedorov committed Aug 14, 2021
1 parent 9fb996b commit 2ef3fee
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 117 deletions.
4 changes: 2 additions & 2 deletions mbstring/mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function mb_substitute_character(string|int|null $substitute_character = null):
* </p>
* @return bool true on success or false on failure.
*/
#[PhpStormStubsElementAvailable(to: '7.4')]
#[PhpStormStubsElementAvailable(from: '5.3', to: '7.4')]
function mb_parse_str(string $string, &$result): bool {}

/**
Expand All @@ -213,7 +213,7 @@ function mb_parse_str(string $string, &$result): bool {}
* </p>
* @return bool true on success or false on failure.
*/
#[PhpStormStubsElementAvailable('8.0')]
#[PhpStormStubsElementAvailable(from: '8.0')]
function mb_parse_str(string $string, &$result): bool {}

/**
Expand Down
18 changes: 18 additions & 0 deletions runTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
echo "Installing composer packages..."
docker-compose -f docker-compose.yml run test_runner composer install -d /opt/project/phpstorm-stubs --ignore-platform-reqs
phpVersions=("7.1" "7.2" "7.3" "7.4" "8.0" "8.1")
for i in "${phpVersions[@]}"
do
export PHP_VERSION=$i
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR" || exit
echo "Building docker container for PHP_$i..."
docker-compose -f docker-compose.yml build >/dev/null
echo "Dumping reflection data to file $SCRIPT_DIR/ReflectionData.json for PHP_$i..."
docker-compose -f docker-compose.yml run -e PHP_VERSION="$i" php_under_test /usr/local/bin/php /opt/project/phpstorm-stubs/tests/Tools/dump-reflection-to-file.php
echo "Running tests agains PHP_$i..."
docker-compose -f docker-compose.yml run -e PHP_VERSION="$i" test_runner /opt/project/phpstorm-stubs/vendor/bin/phpunit --configuration /opt/project/phpstorm-stubs/phpunit.xml --testsuite PHP_"$i"
echo "Removing file $SCRIPT_DIR/ReflectionData.json with reflection data for PHP_$i..."
rm -f "$SCRIPT_DIR/ReflectionData.json"
done
22 changes: 7 additions & 15 deletions tests/BaseClassesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public function testClassesMethodsExist(PHPClass|PHPInterface $class, PHPMethod
} else {
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($className);
}
static::assertArrayHasKey(
$method->name,
$stubClass->methods,
"Missing method $className::$method->name"
);
static::assertNotEmpty($stubClass->getMethod($method->name), "Missing method $className::$method->name");
}

/**
Expand Down Expand Up @@ -155,7 +151,7 @@ function ($parameter) {
public function testClassInterfaces(PHPClass $class)
{
$className = $class->name;
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($class->name, null, false);
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($class->name, shouldSuitCurrentPhpVersion: false);
foreach ($class->interfaces as $interface) {
static::assertContains(
$interface,
Expand All @@ -173,13 +169,9 @@ public function testClassProperties(PHPClass $class, PHPProperty $property)
{
$className = $class->name;
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($class->name);
static::assertArrayHasKey(
$property->name,
$stubClass->properties,
"Missing property $property->access "
static::assertNotEmpty($stubClass->getProperty($property->name), "Missing property $property->access "
. implode('|', $property->typesFromSignature) .
"$className::$$property->name"
);
"$className::$$property->name");
}

/**
Expand Down Expand Up @@ -256,10 +248,10 @@ public function testClassesExist(PHPClass|PHPInterface $class): void
{
$className = $class->name;
if ($class instanceof PHPClass) {
$stubClasses = PhpStormStubsSingleton::getPhpStormStubs()->getClasses();
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($className);
} else {
$stubClasses = PhpStormStubsSingleton::getPhpStormStubs()->getInterfaces();
$stubClass = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($className);
}
static::assertArrayHasKey($className, $stubClasses, "Missing class $className: class $className {}");
static::assertNotEmpty($className, "Missing class $className: class $className {}");
}
}
16 changes: 7 additions & 9 deletions tests/BaseConstantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public function testConstants(PHPConst $constant): void
{
$constantName = $constant->name;
$constantValue = $constant->value;
$stubConstants = PhpStormStubsSingleton::getPhpStormStubs()->getConstants();
static::assertArrayHasKey(
$constantName,
$stubConstants,
$stubConstant = PhpStormStubsSingleton::getPhpStormStubs()->getConstant($constantName);
static::assertNotEmpty(
$stubConstant,
"Missing constant: const $constantName = $constantValue\n"
);
}
Expand All @@ -37,13 +36,12 @@ public function testClassConstants(PHPClass|PHPInterface $class, PHPConst $const
$constantName = $constant->name;
$constantValue = $constant->value;
if ($class instanceof PHPClass) {
$stubConstants = PhpStormStubsSingleton::getPhpStormStubs()->getClass($class->name)->constants;
$stubConstant = PhpStormStubsSingleton::getPhpStormStubs()->getClass($class->name)->getConstant($constantName);
} else {
$stubConstants = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($class->name)->constants;
$stubConstant = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($class->name)->getConstant($constantName);
}
static::assertArrayHasKey(
$constantName,
$stubConstants,
static::assertNotEmpty(
$stubConstant,
"Missing constant: const $constantName = $constantValue\n"
);
}
Expand Down
19 changes: 9 additions & 10 deletions tests/BaseFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ class BaseFunctionsTest extends BaseStubsTest
{
/**
* @dataProvider \StubTests\TestData\Providers\Reflection\ReflectionFunctionsProvider::allFunctionsProvider
* @throws Exception
* @throws Exception|RuntimeException
*/
public function testFunctionsExist(PHPFunction $function): void
{
$functionName = $function->name;
$stubFunctions = PhpStormStubsSingleton::getPhpStormStubs()->getFunctions();
$stubFunction = PhpStormStubsSingleton::getPhpStormStubs()->getFunction($functionName);
$params = BaseStubsTest::getParameterRepresentation($function);
static::assertArrayHasKey($functionName, $stubFunctions, "Missing function: function $functionName($params){}");
static::assertNotEmpty($stubFunction, "Missing function: function $functionName($params){}");
}

/**
* @dataProvider \StubTests\TestData\Providers\Reflection\ReflectionFunctionsProvider::functionsForDeprecationTestsProvider
* @throws RuntimeException
*/
public function testFunctionsDeprecation(PHPFunction $function)
{
$functionName = $function->name;
$stubFunctions = PhpStormStubsSingleton::getPhpStormStubs()->getFunctions();
$phpstormFunction = $stubFunctions[$functionName];
$stubFunction = PhpStormStubsSingleton::getPhpStormStubs()->getFunction($functionName);
static::assertFalse(
$function->is_deprecated && $phpstormFunction->is_deprecated !== true,
$function->is_deprecated && $stubFunction->is_deprecated !== true,
"Function $functionName is not deprecated in stubs"
);
}
Expand All @@ -50,10 +50,9 @@ public function testFunctionsDeprecation(PHPFunction $function)
public function testFunctionsParametersAmount(PHPFunction $function)
{
$functionName = $function->name;
$stubFunctions = PhpStormStubsSingleton::getPhpStormStubs()->getFunctions();
$phpstormFunction = $stubFunctions[$functionName];
$stubFunction = PhpStormStubsSingleton::getPhpStormStubs()->getFunction($functionName);
$filteredStubParameters = array_filter(
$phpstormFunction->parameters,
$stubFunction->parameters,
fn ($parameter) => BasePHPElement::entitySuitsCurrentPhpVersion($parameter)
);
$uniqueParameterNames = array_unique(array_map(fn (PHPParameter $parameter) => $parameter->name, $filteredStubParameters));
Expand All @@ -63,7 +62,7 @@ public function testFunctionsParametersAmount(PHPFunction $function)
$uniqueParameterNames,
"Parameter number mismatch for function $functionName.
Expected: " . BaseStubsTest::getParameterRepresentation($function) . "\n" .
'Actual: ' . BaseStubsTest::getParameterRepresentation($phpstormFunction)
'Actual: ' . BaseStubsTest::getParameterRepresentation($stubFunction)
);
}

Expand Down
13 changes: 3 additions & 10 deletions tests/BaseStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public static function getStringRepresentationOfDefaultParameterValue(mixed $def
if ($defaultValue instanceof ConstFetch) {
$defaultValueName = (string)$defaultValue->name;
if ($defaultValueName !== 'false' && $defaultValueName !== 'true' && $defaultValueName !== 'null') {
$constants = array_filter(
PhpStormStubsSingleton::getPhpStormStubs()->getConstants(),
fn (PHPConst $const) => $const->name === (string)$defaultValue->name
);
/** @var PHPConst $constant */
$constant = array_pop($constants);
$constant = PhpStormStubsSingleton::getPhpStormStubs()->getConstant($defaultValueName);
$value = $constant->value;
} else {
$value = $defaultValueName;
Expand Down Expand Up @@ -87,9 +82,7 @@ public static function getStringRepresentationOfDefaultParameterValue(mixed $def
if ((string)$defaultValue->name === 'class') {
$value = (string)$defaultValue->class;
} else {
$constants = array_filter($parentClass->constants, fn (PHPConst $const) => $const->name === (string)$defaultValue->name);
/** @var PHPConst $constant */
$constant = array_pop($constants);
$constant = $parentClass->getConstant((string)$defaultValue->name);;
$value = $constant->value;
}
} else {
Expand Down Expand Up @@ -127,7 +120,7 @@ protected static function getDuplicatedFunctions(array $filtered): array
$duplicatedFunctions = array_filter($filtered, function (PHPFunction $value, int|string $key) {
$duplicatesOfFunction = self::getAllDuplicatesOfFunction($value->name);
$functionVersions[] = ParserUtils::getAvailableInVersions(
PhpStormStubsSingleton::getPhpStormStubs()->getFunction($value->name)
PhpStormStubsSingleton::getPhpStormStubs()->getFunction($value->name, shouldSuitCurrentPhpVersion: false)
);
array_push($functionVersions, ...array_values(array_map(
fn (PHPFunction $function) => ParserUtils::getAvailableInVersions($function),
Expand Down
3 changes: 1 addition & 2 deletions tests/Model/PHPMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ReflectionMethod;
use RuntimeException;
use stdClass;
use StubTests\Parsers\ParserUtils;

class PHPMethod extends PHPFunction
{
Expand Down Expand Up @@ -77,7 +76,7 @@ public function readObjectFromStubNode($node)
$index = 0;
foreach ($node->getParams() as $parameter) {
$parsedParameter = (new PHPParameter())->readObjectFromStubNode($parameter);
if (in_array(doubleval(getenv('PHP_VERSION')), ParserUtils::getAvailableInVersions($parsedParameter))) {
if (self::entitySuitsCurrentPhpVersion($parsedParameter)) {
$parsedParameter->indexInSignature = $index;
$this->parameters[] = $parsedParameter;
$index++;
Expand Down
2 changes: 1 addition & 1 deletion tests/Model/PHPProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PHPProperty extends BasePHPElement

/** @var string[] */
public $typesFromSignature = [];
/** @var string[] */
/** @var string[][] */
public $typesFromAttribute = [];
/** @var string[] */
public $typesFromPhpDoc = [];
Expand Down
29 changes: 17 additions & 12 deletions tests/Model/StubsContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,27 @@ public function getFunctions(): array
/**
* @param string $name
* @param string|null $sourceFilePath
* @param bool $shouldSuitCurrentPhpVersion
* @return PHPFunction|null
* @throws RuntimeException
*/
public function getFunction(string $name, ?string $sourceFilePath = null): ?PHPFunction
public function getFunction(string $name, ?string $sourceFilePath = null, bool $shouldSuitCurrentPhpVersion = true): ?PHPFunction
{
$functions = array_filter($this->functions, function (PHPFunction $function) use ($name): bool {
return $function->name === $name && $function->duplicateOtherElement === false
&& BasePHPElement::entitySuitsCurrentPhpVersion($function);
$functions = array_filter($this->functions, function (PHPFunction $function) use ($shouldSuitCurrentPhpVersion, $name): bool {
return $function->name === $name && (!$shouldSuitCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($function));
});
if (count($functions) > 1) {
$functions = array_filter($functions, function (PHPFunction $function): bool {
return $function->duplicateOtherElement === false;
});
}
if (count($functions) === 1) {
return array_pop($functions);
} else {
if ($sourceFilePath !== null) {
$functions = array_filter($functions, function (PHPFunction $function) use ($sourceFilePath) {
$functions = array_filter($functions, function (PHPFunction $function) use ($shouldSuitCurrentPhpVersion, $sourceFilePath) {
return $function->sourceFilePath === $sourceFilePath
&& BasePHPElement::entitySuitsCurrentPhpVersion($function);
&& (!$shouldSuitCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($function));
});
}
if (count($functions) > 1) {
Expand Down Expand Up @@ -146,23 +151,23 @@ public function getClasses(): array
/**
* @param string $name
* @param string|null $sourceFilePath
* @param bool $shouldSuiteCurrentPhpVersion
* @param bool $shouldSuitCurrentPhpVersion
* @return PHPClass|null
* @throws RuntimeException
*/
public function getClass(string $name, ?string $sourceFilePath = null, bool $shouldSuiteCurrentPhpVersion = true): ?PHPClass
public function getClass(string $name, ?string $sourceFilePath = null, bool $shouldSuitCurrentPhpVersion = true): ?PHPClass
{
$classes = array_filter($this->classes, function (PHPClass $class) use ($shouldSuiteCurrentPhpVersion, $name): bool {
$classes = array_filter($this->classes, function (PHPClass $class) use ($shouldSuitCurrentPhpVersion, $name): bool {
return $class->name === $name &&
(!$shouldSuiteCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($class));
(!$shouldSuitCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($class));
});
if (count($classes) === 1) {
return array_pop($classes);
} else {
if ($sourceFilePath !== null) {
$classes = array_filter($classes, function (PHPClass $class) use ($shouldSuiteCurrentPhpVersion, $sourceFilePath) {
$classes = array_filter($classes, function (PHPClass $class) use ($shouldSuitCurrentPhpVersion, $sourceFilePath) {
return $class->sourceFilePath === $sourceFilePath &&
(!$shouldSuiteCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($class));
(!$shouldSuitCurrentPhpVersion || BasePHPElement::entitySuitsCurrentPhpVersion($class));
});
}
if (count($classes) > 1) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Parsers/ParserUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static function getLatestAvailableVersionFromPhpDoc(BasePHPElement $elem
*/
private static function getSinceVersionsFromParentClass(PHPMethod|PHPConst $element): array
{
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($element->parentName, shouldSuiteCurrentPhpVersion: false);
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($element->parentName, shouldSuitCurrentPhpVersion: false);
if ($parentClass === null) {
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($element->parentName, shouldSuitCurrentPhpVersion: false);
}
Expand All @@ -144,7 +144,7 @@ private static function getSinceVersionsFromParentClass(PHPMethod|PHPConst $elem
*/
public static function getLatestAvailableVersionsFromParentClass(PHPMethod|PHPConst $element): array
{
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($element->parentName, shouldSuiteCurrentPhpVersion: false);
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getClass($element->parentName, shouldSuitCurrentPhpVersion: false);
if ($parentClass === null) {
$parentClass = PhpStormStubsSingleton::getPhpStormStubs()->getInterface($element->parentName, shouldSuitCurrentPhpVersion: false);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/StubsConstantsAndParametersValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function testConstantsValues(PHPConst $constant): void
{
$constantName = $constant->name;
$constantValue = $constant->value;
$stubConstants = PhpStormStubsSingleton::getPhpStormStubs()->getConstants();
$stubConstant = PhpStormStubsSingleton::getPhpStormStubs()->getConstant($constantName);
self::assertEquals(
$constantValue,
$stubConstants[$constantName]->value,
$stubConstant->value,
"Constant value mismatch: const $constantName \n
Expected value: $constantValue but was {$stubConstants[$constantName]->value}"
Expected value: $constantValue but was $stubConstant->value"
);
}

Expand Down
Loading

0 comments on commit 2ef3fee

Please sign in to comment.