diff --git a/.phive/phars.xml b/.phive/phars.xml index 74cb369..54f64a1 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/docs/api/classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html b/docs/api/classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html new file mode 100644 index 0000000..0e8e626 --- /dev/null +++ b/docs/api/classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html @@ -0,0 +1,190 @@ + + + + + Documentation + + + + + + + + + + + + + + + +
+

Documentation

+ + + + + +
+ +
+
+ + + + +
+ + +
+

+ TypedArrayIntKeyTest + + + extends TestCase + + + + +

+ + + + + + + + + + + + +

+ Table of Contents + +

+ +
+
+ testAssignValue() + +  : void +
+
+ +
+ + + + + + + +
+

+ Methods + +

+
+

+ testAssignValue() + +

+ + + + + public + testAssignValue() : void + + + + + +
Return values
+ void + — +
+ + +
+
+ +
+
+
+
+

Search results

+ +
+
+
    +
    +
    +
    +
    +
    + + +
    + + + + diff --git a/docs/api/classes/Ngmy-TypedArray-TypedArray.html b/docs/api/classes/Ngmy-TypedArray-TypedArray.html index 1b814c6..ef1fb31 100644 --- a/docs/api/classes/Ngmy-TypedArray-TypedArray.html +++ b/docs/api/classes/Ngmy-TypedArray-TypedArray.html @@ -412,7 +412,7 @@

    withIntKey() -  : TypedArray<int, mixed> +  : TypedArray<int|null, mixed>
    Returns a new instance of the typed array with the int type key.
    @@ -891,7 +891,7 @@

    @@ -939,7 +939,7 @@

    @@ -1064,7 +1064,7 @@

    @@ -1100,15 +1100,6 @@

    - -
    - phpstan-param -
    -
    - -

    TKey $key

    -
    -
    @@ -1132,7 +1123,7 @@

    @@ -1168,15 +1159,6 @@

    - -
    - phpstan-param -
    -
    - -

    TKey $key

    -
    -
    phpstan-return @@ -1209,7 +1191,7 @@

    @@ -1294,7 +1276,7 @@

    @@ -1330,15 +1312,6 @@

    - -
    - phpstan-param -
    -
    - -

    TKey $key

    -
    -
    @@ -1995,7 +1968,7 @@

    public - withIntKey() : TypedArray<int, mixed> + withIntKey() : TypedArray<int|null, mixed>
    @@ -2011,14 +1984,14 @@

    -

    TypedArray<int, TValue>

    +

    TypedArray<int|null, TValue>

    Return values
    - TypedArray<int, mixed> + TypedArray<int|null, mixed>
    @@ -2620,7 +2593,7 @@

    @@ -2683,7 +2656,7 @@

    @@ -2706,21 +2679,6 @@

    Parameters
    -
    - Tags - -
    -
    -
    - phpstan-param -
    -
    - -

    TKey $key

    -
    - -
    -
    Return values
    int|string|null diff --git a/docs/api/files/tests-typedarrayintkeytest.html b/docs/api/files/tests-typedarrayintkeytest.html new file mode 100644 index 0000000..691ca11 --- /dev/null +++ b/docs/api/files/tests-typedarrayintkeytest.html @@ -0,0 +1,123 @@ + + + + + Documentation + + + + + + + + + + + + + + + +
    +

    Documentation

    + + + + + +
    + +
    +
    + + + + +
    +
      +
    + +
    +

    TypedArrayIntKeyTest.php

    + + + +

    + Interfaces, Classes and Traits + +

    + +
    + +
    TypedArrayIntKeyTest
    +
    + +
    + + + + + + + +
    +
    +
    +
    +

    Search results

    + +
    +
    +
      +
      +
      +
      +
      +
      + + +
      + + + + diff --git a/docs/api/indices/files.html b/docs/api/indices/files.html index 84dd052..f8ee30c 100644 --- a/docs/api/indices/files.html +++ b/docs/api/indices/files.html @@ -106,6 +106,7 @@

      T

    • TypedArray.php
    • TypedArrayClassKeyTest.php
    • TypedArrayClassValueTest.php
    • +
    • TypedArrayIntKeyTest.php
    • TypedArrayInterfaceKeyTest.php
    • TypedArrayInterfaceValueTest.php
    • TypedArrayPrimitiveKeyTest.php
    • diff --git a/docs/api/js/searchIndex.js b/docs/api/js/searchIndex.js index dfb4bc8..f5b0e60 100644 --- a/docs/api/js/searchIndex.js +++ b/docs/api/js/searchIndex.js @@ -425,6 +425,16 @@ Search.appendIndex( "name": "createInstance", "summary": "", "url": "classes/Ngmy-TypedArray-Tests-TypedArrayInterfaceValueTest.html#method_createInstance" + }, { + "fqsen": "\\Ngmy\\TypedArray\\Tests\\TypedArrayIntKeyTest", + "name": "TypedArrayIntKeyTest", + "summary": "", + "url": "classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html" + }, { + "fqsen": "\\Ngmy\\TypedArray\\Tests\\TypedArrayIntKeyTest\u003A\u003AtestAssignValue\u0028\u0029", + "name": "testAssignValue", + "summary": "", + "url": "classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html#method_testAssignValue" }, { "fqsen": "\\Ngmy\\TypedArray\\Tests\\TypedArrayPrimitiveKeyTest", "name": "TypedArrayPrimitiveKeyTest", diff --git a/docs/api/namespaces/ngmy-typedarray-tests.html b/docs/api/namespaces/ngmy-typedarray-tests.html index e8cb49b..fe4c50d 100644 --- a/docs/api/namespaces/ngmy-typedarray-tests.html +++ b/docs/api/namespaces/ngmy-typedarray-tests.html @@ -108,6 +108,8 @@

      TypedArrayInterfaceKeyTest
      TypedArrayInterfaceValueTest
      +
      +
      TypedArrayIntKeyTest
      TypedArrayPrimitiveKeyTest
      diff --git a/docs/api/packages/Default.html b/docs/api/packages/Default.html index 58f7a53..f83aa40 100644 --- a/docs/api/packages/Default.html +++ b/docs/api/packages/Default.html @@ -120,6 +120,8 @@

      TypedArrayInterfaceKeyTest
      TypedArrayInterfaceValueTest
      +
      +
      TypedArrayIntKeyTest
      TypedArrayPrimitiveKeyTest
      diff --git a/docs/examples/example_phpstan1.php b/docs/examples/example_phpstan1.php index e11cefe..53aba02 100644 --- a/docs/examples/example_phpstan1.php +++ b/docs/examples/example_phpstan1.php @@ -2,7 +2,7 @@ declare(strict_types=1); -require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/../../vendor/autoload.php'; $arrayArray = Ngmy\TypedArray\TypedArray::new()->withArrayValue(); $arrayArray[] = []; // Good @@ -53,4 +53,10 @@ $Trait1Array = Ngmy\TypedArray\TypedArray::new()->withTraitValue(Ngmy\TypedArray\Tests\Data\Trait1::class); $Trait1Array[] = new Ngmy\TypedArray\Tests\Data\Class3(); // Good $Trait1Array[] = new Ngmy\TypedArray\Tests\Data\Class1(); // Good. This is the false negative -$Trait1Array[] = null; // No Good +$Trait1Array[] = null; // No good + +$intKeyArray = Ngmy\TypedArray\TypedArray::new()->withIntKey(); +$intKeyArray[0] = 0; // Good +$intKeyArray[-1] = -1; // Good +$intKeyArray[] = 1; // Good +$intKeyArray['1'] = 1; // No good diff --git a/docs/examples/example_phpstan2.php b/docs/examples/example_phpstan2.php index d7248a5..1c771c4 100644 --- a/docs/examples/example_phpstan2.php +++ b/docs/examples/example_phpstan2.php @@ -2,7 +2,7 @@ declare(strict_types=1); -require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/../../vendor/autoload.php'; PHPStan\dumpType(Ngmy\TypedArray\TypedArray::new()); // Ngmy\TypedArray\TypedArray PHPStan\dumpType(Ngmy\TypedArray\TypedArray::new()->withBoolKey()); // Ngmy\TypedArray\TypedArray diff --git a/docs/examples/example_readme.php b/docs/examples/example_readme.php index b9239a7..8b40948 100644 --- a/docs/examples/example_readme.php +++ b/docs/examples/example_readme.php @@ -2,7 +2,7 @@ declare(strict_types=1); -require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/../../vendor/autoload.php'; // Returns a new instance of the typed array with the int type value $intArray = Ngmy\TypedArray\TypedArray::new()->withIntValue(); // TypedArray diff --git a/src/TypedArray.php b/src/TypedArray.php index a3c868a..70dc0fd 100644 --- a/src/TypedArray.php +++ b/src/TypedArray.php @@ -132,9 +132,9 @@ public function withFloatKey(): self /** * Returns a new instance of the typed array with the int type key. * - * @return TypedArray + * @return TypedArray * - * @phpstan-return TypedArray + * @phpstan-return TypedArray */ public function withIntKey(): self { @@ -551,8 +551,6 @@ public function toArray(): array /** * @param mixed $key * @see https://www.php.net/manual/en/arrayaccess.offsetexists.php - * - * @phpstan-param TKey $key */ public function offsetExists($key): bool { @@ -564,7 +562,6 @@ public function offsetExists($key): bool * @return mixed * @see https://www.php.net/manual/en/arrayaccess.offsetget.php * - * @phpstan-param TKey $key * @phpstan-return TValue|null */ public function offsetGet($key) @@ -622,14 +619,14 @@ public function offsetSet($key, $value): void /** * @param mixed $key * @see https://www.php.net/manual/en/arrayaccess.offsetunset.php - * - * @phpstan-param TKey $key */ public function offsetUnset($key): void { $keyHashCode = $this->getKeyHashCode($key); unset($this->values[$keyHashCode]); - unset($this->keys[$keyHashCode]); + if ($this->keyType == self::KEY_TYPES['object'] || !\is_null($this->keyClassKind)) { + unset($this->keys[$keyHashCode]); + } } /** @@ -666,8 +663,6 @@ private function __construct( /** * @param mixed $key * @return int|string|null - * - * @phpstan-param TKey $key */ private function getKeyHashCode($key) { diff --git a/tests/TypedArrayIntKeyTest.php b/tests/TypedArrayIntKeyTest.php new file mode 100644 index 0000000..f584be7 --- /dev/null +++ b/tests/TypedArrayIntKeyTest.php @@ -0,0 +1,28 @@ +withIntKey(); + $intKeyArray[1] = 1; + $intKeyArray[100] = 100; + $intKeyArray[-1] = -1; + $intKeyArray[] = 101; + $intKeyArray[] = 102; + + $this->assertEquals([ + 1 => 1, + 100 => 100, + -1 => -1, + 101 => 101, + 102 => 102, + ], $intKeyArray->toArray()); + } +} diff --git a/tests/TypedArrayPrimitiveKeyTest.php b/tests/TypedArrayPrimitiveKeyTest.php index d8efb08..b50b82d 100644 --- a/tests/TypedArrayPrimitiveKeyTest.php +++ b/tests/TypedArrayPrimitiveKeyTest.php @@ -181,6 +181,12 @@ public function test(string $keyType, ?array $keys, $expected): void } $this->assertEquals($expected, \array_values($typedArray->toArray())); + + foreach ($keys as $key) { + unset($typedArray[$key]); + $this->assertFalse(isset($typedArray[$key])); + $this->assertTrue(empty($typedArray[$key])); + } } /**