From 98eaee995064baa22cde0eeed2e83a5df455d1c9 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 17:50:18 +0900 Subject: [PATCH 1/8] Fix require paths --- docs/examples/example_phpstan1.php | 2 +- docs/examples/example_phpstan2.php | 2 +- docs/examples/example_readme.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/example_phpstan1.php b/docs/examples/example_phpstan1.php index e11cefe..bfd8bf6 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 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 From 89d1d6fbdf20537ab1d616ea16dea9399f542db3 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:47:49 +0900 Subject: [PATCH 2/8] Fix types --- src/TypedArray.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/TypedArray.php b/src/TypedArray.php index a3c868a..36fb6b3 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,8 +619,6 @@ 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 { @@ -666,8 +661,6 @@ private function __construct( /** * @param mixed $key * @return int|string|null - * - * @phpstan-param TKey $key */ private function getKeyHashCode($key) { From 216630a72345fd7af009671c31b237c9dfe18bb1 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:48:22 +0900 Subject: [PATCH 3/8] Fix to call the method only when necessary --- src/TypedArray.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TypedArray.php b/src/TypedArray.php index 36fb6b3..70dc0fd 100644 --- a/src/TypedArray.php +++ b/src/TypedArray.php @@ -624,7 +624,9 @@ 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]); + } } /** From bf27044cf38dac7077db36e021826afd20044470 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:52:22 +0900 Subject: [PATCH 4/8] Add the test --- tests/TypedArrayIntKeyTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/TypedArrayIntKeyTest.php 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()); + } +} From 162f5d542140a5cd4c1018fbb70b45cf025235a9 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:54:49 +0900 Subject: [PATCH 5/8] Add the PHPStan example --- docs/examples/example_phpstan1.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/examples/example_phpstan1.php b/docs/examples/example_phpstan1.php index bfd8bf6..53aba02 100644 --- a/docs/examples/example_phpstan1.php +++ b/docs/examples/example_phpstan1.php @@ -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 From 2d5f313a4a67c67bcce877f5b74bb9702a2f6bc5 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:56:50 +0900 Subject: [PATCH 6/8] Update tools --- .phive/phars.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 @@ - + - - + + From 599d1762025e8739c04828d115920a1b94f32361 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 20:57:03 +0900 Subject: [PATCH 7/8] Generate the API documentation --- ...TypedArray-Tests-TypedArrayIntKeyTest.html | 190 ++++++++++++++++++ .../classes/Ngmy-TypedArray-TypedArray.html | 66 ++---- .../api/files/tests-typedarrayintkeytest.html | 123 ++++++++++++ docs/api/indices/files.html | 1 + docs/api/js/searchIndex.js | 10 + .../api/namespaces/ngmy-typedarray-tests.html | 2 + docs/api/packages/Default.html | 2 + 7 files changed, 340 insertions(+), 54 deletions(-) create mode 100644 docs/api/classes/Ngmy-TypedArray-Tests-TypedArrayIntKeyTest.html create mode 100644 docs/api/files/tests-typedarrayintkeytest.html 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
      From d63b5875a7d7661eec838512a75084a67abbfc64 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Thu, 18 Mar 2021 21:06:04 +0900 Subject: [PATCH 8/8] Add test cases to increase coverage --- tests/TypedArrayPrimitiveKeyTest.php | 6 ++++++ 1 file changed, 6 insertions(+) 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])); + } } /**