Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tribe__Cache::has method #2061

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Add Tribe__Cache::has method #2061

wants to merge 4 commits into from

Conversation

lucatume
Copy link
Contributor

@lucatume lucatume commented Mar 15, 2024

🎫 Ticket

Innovation Day work, Cookbook

🗒️ Description

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.

✔️ Checklist

  • Changelog entry in the readme.txt file.
  • Code is covered by NEW wpunit or integration tests.
  • Code is covered by EXISTING wpunit or integration tests.
  • Are all the required tests passing?
  • Automated code review comments are addressed.
  • Have you added Artifacts?
  • Check the base branch for your PR.
  • Add your PR to the project board for the release.

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.
@lucatume lucatume self-assigned this Mar 15, 2024
*
* @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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket
Expected 1 spaces after opening parenthesis; 0 found

Copy link
Contributor

@tec-bot tec-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
Squiz.Commenting.FunctionCommentThrowTag.Missing
Missing @throws tag in function comment

*
* @return bool
*/
public static function has_shape( $array, array $shape ): bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
When a multi-item array uses associative keys, each value should start on a new line.

@lucatume lucatume force-pushed the feat/tribe-cache-has branch from 7032a98 to a9bc11e Compare March 15, 2024 16:36
],
'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' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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' ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.

@lucatume lucatume force-pushed the feat/tribe-cache-has branch from a9bc11e to fa40956 Compare March 15, 2024 16:56
*
* @return bool Whether the array has the specified shape.
*/
public static function has_shape( $array, array $shape, bool $strict = false ): bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
Multi-line arrays must have a trailing comma after the last element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants