Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed May 23, 2022
1 parent cba4ab1 commit 48f300a
Show file tree
Hide file tree
Showing 58 changed files with 1,070 additions and 1,062 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'idea'
id 'org.jetbrains.intellij' version '1.5.3' // https://github.com/JetBrains/gradle-intellij-plugin
id 'org.jetbrains.intellij' version '1.5.2' // https://github.com/JetBrains/gradle-intellij-plugin
id 'org.jetbrains.kotlin.jvm' version '1.6.10' // https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ class ResolvingGenericFunctionCall(project: Project) : ResolvingGenericBase(proj

val remainingPackedData = packedData.substring(firstSeparatorIndex + "@@".length)
val secondSeparatorIndex = remainingPackedData.indexOf("@@")
if (secondSeparatorIndex == -1) {
return false
}
val explicitGenericsString = remainingPackedData.substring(0, secondSeparatorIndex)
val argumentsTypesString = remainingPackedData.substring(secondSeparatorIndex + "@@".length)

Expand Down Expand Up @@ -678,6 +681,8 @@ abstract class GenericCall(val project: Project) {
if (countGenericNames == countExplicitSpecs && countExplicitSpecs == countImplicitSpecs) {
var isEqual = true
explicitSpecs.forEachIndexed { index, explicitSpec ->
// TODO: Здесь должна быть проверка что они не равны
// Такой подход не работает для nullable
if (!implicitSpecs[index].isAssignableFrom(explicitSpec, project)) {
isEqual = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ class InlayHintsCollector(
call.genericNames()
} else {
call.function()!!.genericNames()
}.joinToString(", ") { it.name + if (it.extendsClass != null) ": " + it.extendsClass else "" }
}.joinToString(", ") {
val extendsHint = if (it.extendsClass != null)
": " + it.extendsClass.removePrefix("\\")
else
""
it.name +extendsHint
}

if (genericNames.isEmpty()) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GenericUnnecessaryExplicitInstantiationListInspection : PhpInspection() {
holder.registerProblem(
call.explicitSpecsPsi!!,
"Remove unnecessary explicit list of instantiation arguments",
ProblemHighlightType.WEAK_WARNING,
ProblemHighlightType.WARNING,
RemoveExplicitGenericSpecsQuickFix()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class KphpDocTagGenericPsiImpl : PhpDocTagImpl, KphpDocTagImpl {
val parts = type.split(':')
parts[0] to parts[1]
} else {
type to ""
type to null
}
KphpDocGenericParameterDecl(name, extendsClass)
}
Expand Down
File renamed without changes.
122 changes: 122 additions & 0 deletions src/test/fixtures/generics/.meta/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* @param any $expr
*/
function expr_type($expr, string $string) {
}

/**
* @kphp-generic T
* @param T $arg
* @return T
*/
function nullable_of($arg) {
if (0) {
return null;
}
return $arg;
}

/**
* @param mixed $args
*/
function tuple(...$args) {
return ${"args"};
}
/**
* @param mixed $args
*/
function shape($args) {
return ${"args"};
}

/**
* @kphp-generic T
* @param T ...$els
* @return SimpleVector<T>
*/
function listOf(...$els) {
return new SimpleVector(...$els);
}

/**
* @kphp-generic T
* @param T $arg
* @return T
*/
function mirror($arg) {
return $arg;
}

/**
* @kphp-generic T1, T2
* @param T1 $a1
* @param T2 $a2
* @return T1|T2
*/
function combine($a1, $a2) {
if (0) {
return $a2;
}
return $a1;
}

/**
* @kphp-generic T1, T2
* @param T1[] $array
* @param class-string<T2> $class
* @return T2[]
*/
function filter_is_instance($array, $class) {
return array_filter($array, fn($el) => is_a($el, $class));;
}

/**
* @kphp-generic T
* @param callable(T,T): bool $gt
* @param T ...$arr
* @return T
*/
function max_by($gt, ...$arr) {
$max = array_first_value($arr);
for ($i = 1; $i < count($arr); ++$i) {
if ($gt($arr[$i], $max)) {
$max = $arr[$i];
}
}
return $max;
}

/**
* @kphp-generic T, DstClass
* @param T $obj
* @param class-string<DstClass> $to_classname
* @return DstClass
*/
function instance_cast($obj, $to_classname) {
return $obj;
}

/**
* @kphp-generic TElem, ToName
* @param TElem[] $arr
* @param class-string<ToName> $to
* @return ToName[]
*/
function array_cast($arr, $to) {
$out = [];
foreach ($arr as $k => $v) {
$out[$k] = instance_cast($v, $to);
}
return $out;
}

/**
* @kphp-generic T
* @param T[] $arr
* @return T
*/
function array_first_value(array $arr) {
return $arr[0];
}
19 changes: 19 additions & 0 deletions src/test/fixtures/generics/Containers/MutableVectorList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Mutable generic collection.
*
* @kphp-generic T
*/
class MutableList extends VectorList {
/**
* @param T $data
*/
public function add($data): void {
$this->data[] = $data;
}

public function remove(int $index): void {
unset($this->data[$index]);
}
}
35 changes: 35 additions & 0 deletions src/test/fixtures/generics/Containers/Pair.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @kphp-generic T1, T2
*/
class Pair {
/** @var T1 */
private $first;

/** @var T2 */
private $second;

/**
* @param T1 $first
* @param T2 $second
*/
public function __construct($first, $second) {
$this->first = $first;
$this->second = $second;
}

/**
* @return T1
*/
public function first() {
return $this->first;
}

/**
* @return T2
*/
public function second() {
return $this->second;
}
}
35 changes: 35 additions & 0 deletions src/test/fixtures/generics/Containers/SerializableKeyMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @kphp-generic TKey: Serializable, TValue
*/
class SerializableKeyMap {
/** @var TValue[] */
private $data = [];

/**
* @param Pair<TKey, TValue> ...$els
*/
public function __construct(...$els) {
foreach ($els as $keyValue) {
$key = $keyValue->first();
$this->data[$key->serialize()] = $keyValue->second();
}
}

/**
* @param TKey $key
* @return TValue
*/
public function get($key) {
return $this->data[$key->serialize()];
}

/**
* @param TKey $key
* @param TValue $value
*/
public function set($key, $value): void {
$this->data[$key->serialize()] = $key;
}
}
35 changes: 35 additions & 0 deletions src/test/fixtures/generics/Containers/SimpleMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @kphp-generic TKey, TValue
*/
class SimpleMap {
/** @var TValue[] */
private $data = [];

/**
* @param Pair<TKey, TValue> ...$els
*/
public function __construct(...$els) {
foreach ($els as $keyValue) {
$key = $keyValue->first();
$this->data[$key->serialize()] = $keyValue->second();
}
}

/**
* @param TKey $key
* @return TValue
*/
public function get($key) {
return $this->data[$key->serialize()];
}

/**
* @param TKey $key
* @param TValue $value
*/
public function set($key, $value): void {
$this->data[$key->serialize()] = $key;
}
}
37 changes: 37 additions & 0 deletions src/test/fixtures/generics/Containers/SimpleVector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @kphp-generic T
*/
class SimpleVector {
/** @var T[] */
private $data = [];

/**
* @param T ...$els
*/
public function __construct(...$els) {
$this->data = $els;
}

/**
* @return T
*/
public function get(int $index) {
return $this->data[$index];
}

/**
* @param T $data
*/
public function add($data): void {
$this->data[] = $data;
}

/**
* @return T[]
*/
public function raw(): array {
return $this->data;
}
}
Loading

0 comments on commit 48f300a

Please sign in to comment.