Add Tribe__Cache::has method - #2061
Conversation
For back-compatibility purposes, I've not modified existing methods like `offsetGet`. This add a `$found` flag, set by reference, to the `get` method similarly to the one set by the `wp_cache_get` function. To just check on a value, I've added the `has` method.
| * @return mixed | ||
| */ | ||
| public function get( $id, $expiration_trigger = '', $default = false, $expiration = 0, $args = [] ) { | ||
| public function get( $id, $expiration_trigger = '', $default = false, $expiration = 0, $args = [], ?bool &$found = false ) { |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound
It is recommended not to use reserved keyword "default" as function parameter name. Found: $default
|
|
||
| $cache->set( 'foo-bar', 'bar' ); | ||
| $this->assertTrue( $cache->has( 'foo-bar' ) ); | ||
| $this->assertEquals('bar', $cache->get( 'foo-bar','', false, 0, [], $found ) ); |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket
Expected 1 spaces after opening parenthesis; 0 found
|
|
||
| $cache->set( 'foo-bar', 'bar' ); | ||
| $this->assertTrue( $cache->has( 'foo-bar' ) ); | ||
| $this->assertEquals('bar', $cache->get( 'foo-bar','', false, 0, [], $found ) ); |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma
No space found after comma in argument list
|
|
||
| $cache->set( 'foo-bar-save-post', 'bar', 0, Triggers::TRIGGER_SAVE_POST ); | ||
| $this->assertTrue( $cache->has( 'foo-bar-save-post', Triggers::TRIGGER_SAVE_POST ) ); | ||
| $this->assertEquals('bar', $cache->get( 'foo-bar-save-post', Triggers::TRIGGER_SAVE_POST, false, 0, [], $found ) ); |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket
Expected 1 spaces after opening parenthesis; 0 found
|
|
||
| $cache->set( 'foo-bar-updated-option', 'bar', 0, Triggers::TRIGGER_UPDATED_OPTION ); | ||
| $this->assertTrue( $cache->has( 'foo-bar-updated-option', Triggers::TRIGGER_UPDATED_OPTION ) ); | ||
| $this->assertEquals('bar', $cache->get( 'foo-bar-updated-option', Triggers::TRIGGER_UPDATED_OPTION, false, 0, [], $found ) ); |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket
Expected 1 spaces after opening parenthesis; 0 found
|
|
||
| $cache->set( 'foo-bar-generate-rewrite-rules', 'bar', 0, Triggers::TRIGGER_GENERATE_REWRITE_RULES ); | ||
| $this->assertTrue( $cache->has( 'foo-bar-generate-rewrite-rules', Triggers::TRIGGER_GENERATE_REWRITE_RULES ) ); | ||
| $this->assertEquals('bar', $cache->get( 'foo-bar-generate-rewrite-rules', Triggers::TRIGGER_GENERATE_REWRITE_RULES, false, 0, [], $found ) ); |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket
Expected 1 spaces after opening parenthesis; 0 found
tec-bot
left a comment
There was a problem hiding this comment.
Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit
phpcs
tests/unit/Tribe/Utils/ArrayTest.php|404 col 100| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|406 col 17| NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
tests/unit/Tribe/Utils/ArrayTest.php|406 col 17| SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
tests/unit/Tribe/Utils/ArrayTest.php|408 col 80| WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, all key pass, strict'" and double arrow; 13 found.
tests/unit/Tribe/Utils/ArrayTest.php|409 col 54| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|410 col 100| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|412 col 17| NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
tests/unit/Tribe/Utils/ArrayTest.php|412 col 17| SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
tests/unit/Tribe/Utils/ArrayTest.php|414 col 80| WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, all key pass, non-strict '" and double arrow; 8 found.
tests/unit/Tribe/Utils/ArrayTest.php|415 col 54| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|416 col 100| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|418 col 17| NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
tests/unit/Tribe/Utils/ArrayTest.php|418 col 17| SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
tests/unit/Tribe/Utils/ArrayTest.php|420 col 80| WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, some key pass, strict'" and double arrow; 12 found.
tests/unit/Tribe/Utils/ArrayTest.php|421 col 49| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|422 col 100| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|424 col 17| NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
tests/unit/Tribe/Utils/ArrayTest.php|424 col 17| SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
tests/unit/Tribe/Utils/ArrayTest.php|426 col 80| WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, some key pass, non-strict'" and double arrow; 8 found.
tests/unit/Tribe/Utils/ArrayTest.php|427 col 49| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|428 col 100| WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
tests/unit/Tribe/Utils/ArrayTest.php|430 col 17| NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
tests/unit/Tribe/Utils/ArrayTest.php|430 col 17| SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
| * The callable must have the signature `fn( mixed $value ) :bool`. | ||
| * | ||
| * @return bool | ||
| */ |
| * | ||
| * @return bool | ||
| */ | ||
| public static function has_shape( $array, array $shape ): bool { |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
It is recommended not to use reserved keyword "array" as function parameter name. Found: $array
|
|
||
| public function has_shape_data_provider(): array { | ||
| return [ | ||
| 'not an array' => [ 'foo', [], true, false ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned
Array double arrow not aligned correctly; expected 29 space(s) between "'not an array'" and double arrow, but found 53.
| public function has_shape_data_provider(): array { | ||
| return [ | ||
| 'not an array' => [ 'foo', [], true, false ], | ||
| 'empty array, empty shape' => [ [], [], true, true ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned
Array double arrow not aligned correctly; expected 17 space(s) between "'empty array, empty shape'" and double arrow, but found 41.
| return [ | ||
| 'not an array' => [ 'foo', [], true, false ], | ||
| 'empty array, empty shape' => [ [], [], true, true ], | ||
| 'empty array, non-empty shape, strict' => [ |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned
Array double arrow not aligned correctly; expected 5 space(s) between "'empty array, non-empty shape, strict'" and double arrow, but found 29.
| true | ||
| ], | ||
| 'non-empty array, closure shape, all key fail failure, strict' => [ | ||
| [ 'foo' => 23, 'bar' => 89 ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| ], | ||
| 'non-empty array, closure shape, all key fail failure, strict' => [ | ||
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| true, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| true, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
| false | ||
| ], | ||
| 'non-empty array, closure shape, all key fail failure, non-strict' => [ | ||
| [ 'foo' => 23, 'bar' => 89 ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
7032a98 to
a9bc11e
Compare
| ], | ||
| 'non-empty array, closure shape, all key fail failure, non-strict' => [ | ||
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
| [ 'foo' => 23, 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
| false, | ||
| false | ||
| ], | ||
| 'non-empty array, closure shape, all key pass, strict' => [ |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, all key pass, strict'" and double arrow; 13 found.
| false | ||
| ], | ||
| 'non-empty array, closure shape, all key pass, strict' => [ | ||
| [ 'foo' => 'hello', 'bar' => 'world' ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| true, | ||
| false | ||
| ], | ||
| 'non-empty array, closure shape, some key pass, non-strict' => [ |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow
Expected 1 space between "'non-empty array, closure shape, some key pass, non-strict'" and double arrow; 8 found.
| false | ||
| ], | ||
| 'non-empty array, closure shape, some key pass, non-strict' => [ | ||
| [ 'foo' => 'hello', 'bar' => 89 ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| ], | ||
| 'non-empty array, closure shape, some key pass, non-strict' => [ | ||
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| true |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| true |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
a9bc11e to
fa40956
Compare
| * | ||
| * @return bool Whether the array has the specified shape. | ||
| */ | ||
| public static function has_shape( $array, array $shape, bool $strict = false ): bool { |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
It is recommended not to use reserved keyword "array" as function parameter name. Found: $array
| [], | ||
| [ 'foo' => 'is_string' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
| [], | ||
| [ 'foo' => 'is_string' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLine
There should be a comma after the last array item in a multi-line array.
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
| false, | ||
| false |
There was a problem hiding this comment.
[phpcs] reported by reviewdog 🐶SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.
🎫 Ticket
Innovation Day work, Cookbook
🗒️ Description
For back-compatibility purposes, I've not modified existing methods like
offsetGet.This add a
$foundflag, set by reference, to thegetmethod similarly to the one set by thewp_cache_getfunction.To just check on a value, I've added the
hasmethod.✔️ Checklist
readme.txtfile.wpunitorintegrationtests.wpunitorintegrationtests.