Fix MODEL-REL-01 and MODEL-ENUMS-01 false positives in code quality rules

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-26 03:25:02 +00:00
parent 209dd72b03
commit 051ab489d0
2 changed files with 80 additions and 8 deletions

View File

@@ -109,13 +109,68 @@ class ModelEnums_CodeQualityRule extends CodeQualityRule_Abstract
// We need to identify the structure: // We need to identify the structure:
// 'field_name' => [ integer_value => ['constant' => ..., 'label' => ...], ... ] // 'field_name' => [ integer_value => ['constant' => ..., 'label' => ...], ... ]
// First, find the top-level keys (any field name allowed, special handling for is_ prefix) // Extract only TOP-LEVEL keys (field definitions) by tracking bracket depth
// We'll look for patterns like 'key' => [ or "key" => [ // This prevents matching nested custom properties like 'permissions' => [...]
$pattern = '/[\'"]([^\'"]+)[\'\"]\s*=>\s*\[/'; $top_level_fields = [];
if (preg_match_all($pattern, $enums_content, $field_matches, PREG_OFFSET_CAPTURE)) { $bracket_depth = 0;
foreach ($field_matches[1] as $field_match) { $in_string = false;
$field = $field_match[0]; $string_char = null;
$offset = $field_match[1]; $i = 0;
$len = strlen($enums_content);
while ($i < $len) {
$char = $enums_content[$i];
// Track string boundaries
if (!$in_string && ($char === "'" || $char === '"')) {
$in_string = true;
$string_char = $char;
} elseif ($in_string && $char === $string_char && ($i === 0 || $enums_content[$i - 1] !== '\\')) {
$in_string = false;
$string_char = null;
}
// Track bracket depth when not in string
if (!$in_string) {
if ($char === '[') {
$bracket_depth++;
} elseif ($char === ']') {
$bracket_depth--;
}
}
// Look for field definitions at depth 1 (inside the outer $enums = [...] bracket)
// Pattern: 'field_name' => [
if ($bracket_depth === 1 && !$in_string) {
// Check if we're at the start of a string key
if ($char === "'" || $char === '"') {
$quote = $char;
$key_start = $i + 1;
$j = $key_start;
// Find the closing quote
while ($j < $len && $enums_content[$j] !== $quote) {
$j++;
}
if ($j < $len) {
$key = substr($enums_content, $key_start, $j - $key_start);
// Check if followed by => [
$after_key = ltrim(substr($enums_content, $j + 1, 20));
if (str_starts_with($after_key, '=>')) {
$after_arrow = ltrim(substr($after_key, 2));
if (str_starts_with($after_arrow, '[')) {
$top_level_fields[] = $key;
}
}
$i = $j; // Skip past the key
}
}
}
$i++;
}
// Now validate each top-level field
foreach ($top_level_fields as $field) {
// No naming convention enforcement - any field name is allowed // No naming convention enforcement - any field name is allowed
// Only requirement: integer keys for values (checked below) // Only requirement: integer keys for values (checked below)
@@ -338,7 +393,6 @@ class ModelEnums_CodeQualityRule extends CodeQualityRule_Abstract
} }
} // End of non-boolean field checks } // End of non-boolean field checks
} }
}
} }
} }
} }

View File

@@ -106,8 +106,26 @@ class ModelRelations_CodeQualityRule extends CodeQualityRule_Abstract
$relationship_methods = []; $relationship_methods = [];
$complex_relationships = []; $complex_relationships = [];
// Track multi-line comment state
$in_multiline_comment = false;
// Parse each method to find relationships // Parse each method to find relationships
foreach ($lines as $i => $line) { foreach ($lines as $i => $line) {
// Track multi-line comment state
// Check for comment start (but not if it's closed on same line)
if (preg_match('#/\*#', $line) && !preg_match('#/\*.*\*/#', $line)) {
$in_multiline_comment = true;
}
// Check for comment end
if (preg_match('#\*/#', $line)) {
$in_multiline_comment = false;
continue; // Skip the closing line too
}
// Skip lines inside multi-line comments
if ($in_multiline_comment) {
continue;
}
// Look for public non-static function declarations // Look for public non-static function declarations
if (preg_match('/public\s+(?!static\s+)function\s+(\w+)\s*\(/', $line, $func_match)) { if (preg_match('/public\s+(?!static\s+)function\s+(\w+)\s*\(/', $line, $func_match)) {
$method_name = $func_match[1]; $method_name = $func_match[1];