Skip to content

fix(lib): correctly mark $allowed_item_values type as nullable array - #375

Open
codepuncher wants to merge 1 commit into
CybotAS:masterfrom
codepuncher:patch-1
Open

fix(lib): correctly mark $allowed_item_values type as nullable array#375
codepuncher wants to merge 1 commit into
CybotAS:masterfrom
codepuncher:patch-1

Conversation

@codepuncher

@codepuncher codepuncher commented Jul 23, 2026

Copy link
Copy Markdown

User description

Fixes #374


CodeAnt-AI Description

Allow the class constant validator to accept a missing allowed-values array

What Changed

  • The validator now accepts a nullable array for allowed item values without triggering a type mismatch
  • Calls that omit the allowed-values list continue to work as expected

Impact

✅ Fewer type errors during class constant validation
✅ Safer validation when allowed values are not provided
✅ More reliable library usage

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

@codeant-ai

codeant-ai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Reviewed your PR f6fccb1 Jul 23, 2026 · 15:57 16:02

Updated in place by CodeAnt AI · last 5 reviews

@codeant-ai

codeant-ai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@codeant-ai codeant-ai Bot added the size:XS This PR changes 0-9 lines, ignoring generated files label Jul 23, 2026
@sonarqubecloud

Copy link
Copy Markdown

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Fix nullable typing for allowed array items in class-constant validator

🐞 Bug fix 🕐 Less than 5 minutes

Grey Divider

AI Description

• Make $allowed_item_values explicitly nullable to match its default value.
• Prevent PHP implicit-nullability deprecations / type errors when argument is omitted.
• Align validator trait signature with its documented array|null contract (Fixes #374).
Diagram

graph TD
  A["Base Addon"] --> B[["Array constant validator"]] --> C("Invalid constant")
  D["Base Widget"] --> B
  subgraph Legend
    direction LR
    _cls["Class"] ~~~ _trait[["Trait method"]] ~~~ _ex("Exception")
  end
Loading
High-Level Assessment

The chosen fix (explicit ?array for a parameter defaulting to null) is the correct and minimal approach. Alternatives considered—defaulting to [] or dropping the type hint—either change semantics (treating omitted vs empty list differently) or weaken type safety without benefit.

Files changed (1) +1 / -1

Bug fix (1) +1 / -1
Class_Constant_Override_Validator_Trait.phpFix implicit-nullability in array-constant validation helper +1/-1

Fix implicit-nullability in array-constant validation helper

• Updates 'validate_required_array_class_constant' to accept '?array $allowed_item_values = null', matching the documented 'array|null' intent. This avoids implicit-nullability issues and makes omitting the argument type-safe.

src/lib/traits/Class_Constant_Override_Validator_Trait.php

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Action required

1. PHP <7.1 parse error 🐞 Bug ☼ Reliability
Description
validate_required_array_class_constant() now uses ?array, which is PHP 7.1+ syntax and will fail
to parse on PHP 5.6/7.0, preventing the plugin from loading at all. This bypasses the plugin’s
runtime minimum-PHP check because the file can’t be parsed first.
Code

src/lib/traits/Class_Constant_Override_Validator_Trait.php[124]

+	protected function validate_required_array_class_constant( $required_array_constant_name, ?array $allowed_item_values = null ) {
Evidence
The trait now contains ?array (PHP 7.1+ syntax), while both the plugin readme and runtime code
explicitly claim/enforce a minimum PHP version of 5.6; on PHP <7.1 the file will not parse, so the
plugin cannot load to even show the compatibility error message.

src/lib/traits/Class_Constant_Override_Validator_Trait.php[118-152]
readme.txt[1-10]
src/lib/Cookiebot_WP.php[23-92]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The PR introduces a nullable array type hint (`?array`) which is not supported in PHP versions below 7.1. The plugin declares support for PHP 5.6, so this will cause a parse error before any runtime compatibility checks execute.

## Issue Context
The parameter already defaults to `null`, so PHP 5.6-compatible options include keeping the old signature (`array $allowed_item_values = null`) and relying on the phpdoc `@param array|null`, or removing the type hint and validating with `is_array()` when non-null.

## Fix Focus Areas
- src/lib/traits/Class_Constant_Override_Validator_Trait.php[118-152]
- readme.txt[1-10]
- src/lib/Cookiebot_WP.php[23-92]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

* @throws Exception
*/
protected function validate_required_array_class_constant( $required_array_constant_name, array $allowed_item_values = null ) {
protected function validate_required_array_class_constant( $required_array_constant_name, ?array $allowed_item_values = null ) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

1. Php <7.1 parse error 🐞 Bug ☼ Reliability

validate_required_array_class_constant() now uses ?array, which is PHP 7.1+ syntax and will fail
to parse on PHP 5.6/7.0, preventing the plugin from loading at all. This bypasses the plugin’s
runtime minimum-PHP check because the file can’t be parsed first.
Agent Prompt
## Issue description
The PR introduces a nullable array type hint (`?array`) which is not supported in PHP versions below 7.1. The plugin declares support for PHP 5.6, so this will cause a parse error before any runtime compatibility checks execute.

## Issue Context
The parameter already defaults to `null`, so PHP 5.6-compatible options include keeping the old signature (`array $allowed_item_values = null`) and relying on the phpdoc `@param array|null`, or removing the type hint and validating with `is_array()` when non-null.

## Fix Focus Areas
- src/lib/traits/Class_Constant_Override_Validator_Trait.php[118-152]
- readme.txt[1-10]
- src/lib/Cookiebot_WP.php[23-92]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

* @throws Exception
*/
protected function validate_required_array_class_constant( $required_array_constant_name, array $allowed_item_values = null ) {
protected function validate_required_array_class_constant( $required_array_constant_name, ?array $allowed_item_values = null ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: This nullable type declaration requires PHP 7.1+, but the plugin declares support for PHP 5.6; on supported 5.6 installs this file will fail to parse and can break plugin loading before runtime version checks execute. Keep the parameter untyped (or use a PHP-5.6-compatible approach) to preserve the documented minimum PHP compatibility. [api mismatch]

Severity Level: Critical 🚨
- ❌ Plugin crashes on PHP 5.6 during addon autoload.
- ❌ Cookiebot_WP PHP version guard never runs on 5.6.
- ⚠️ WordPress admin cannot safely deactivate failing plugin.
Steps of Reproduction ✅
1. Confirm the plugin advertises PHP 5.6 support: `readme.txt` lines 4–7 show “* Requires
PHP: 5.6”, and `src/lib/Cookiebot_WP.php` defines `const COOKIEBOT_MIN_PHP_VERSION =
'5.6.0';` around lines 5–6 (verified via Grep).

2. On a WordPress site running PHP 5.6, install and activate this plugin so WordPress
loads `cookiebot.php` (lines 1–24) which defines plugin constants, then `require_once`
includes `vendor/autoload.php`, `src/lib/helper.php`, and
`src/lib/global-deprecations.php`, and finally calls `\cybot\cookiebot\lib\cookiebot()` at
`cookiebot.php:26`.

3. In `src/lib/helper.php`, the namespaced `cookiebot()` function (around lines 10–18 and
620–637) returns `Cookiebot_WP::instance()`, whose constructor in
`src/lib/Cookiebot_WP.php` immediately calls
`throw_exception_if_php_version_is_incompatible()` and then `cookiebot_init()`, where
`Cookiebot_Addons::instance()` is invoked (seen in `src/lib/Cookiebot_WP.php` around lines
25–37).

4. `Cookiebot_Addons::__construct()` in `src/addons/Cookiebot_Addons.php` (lines 60–94 and
120–140) builds a container that instantiates `Settings_Service`
(`src/lib/Settings_Service.php:5`), which `use`s
`cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon`; Composer’s PSR-4
autoloader then loads `src/addons/controller/addons/Base_Cookiebot_Addon.php`, which in
turn `use`s `cybot\cookiebot\lib\traits\Class_Constant_Override_Validator_Trait` and
`use`s the trait at line 19, triggering inclusion of
`src/lib/traits/Class_Constant_Override_Validator_Trait.php` where
`validate_required_array_class_constant( $required_array_constant_name, ?array
$allowed_item_values = null )` is declared (line 25 in the snippet). On PHP 5.6 this
nullable `?array` parameter is invalid syntax, causing a fatal “unexpected '?'” parse
error at load time before `Cookiebot_WP::throw_exception_if_php_version_is_incompatible()`
can run, breaking plugin initialization on an environment the plugin claims to support.

Fix in Cursor Fix in VSCode Claude

(Use Cmd/Ctrl + Click for best experience)

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** src/lib/traits/Class_Constant_Override_Validator_Trait.php
**Line:** 124:124
**Comment:**
	*Api Mismatch: This nullable type declaration requires PHP 7.1+, but the plugin declares support for PHP 5.6; on supported 5.6 installs this file will fail to parse and can break plugin loading before runtime version checks execute. Keep the parameter untyped (or use a PHP-5.6-compatible approach) to preserve the documented minimum PHP compatibility.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP nullable parameter deprecation notice

1 participant