|
| 1 | +<?php |
| 2 | +/* |
| 3 | + +-------------------------------------------------------------------------+ |
| 4 | + | Copyright (C) 2004-2026 The Cacti Group | |
| 5 | + +-------------------------------------------------------------------------+ |
| 6 | + | Cacti: The Complete RRDtool-based Graphing Solution | |
| 7 | + +-------------------------------------------------------------------------+ |
| 8 | +*/ |
| 9 | + |
| 10 | +describe('PHP 7.4 compatibility in audit', function () { |
| 11 | + $files = array( |
| 12 | + 'audit.php', |
| 13 | + 'audit_functions.php', |
| 14 | + 'setup.php', |
| 15 | + ); |
| 16 | + |
| 17 | + it('does not use str_contains (PHP 8.0)', function () use ($files) { |
| 18 | + foreach ($files as $f) { |
| 19 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 20 | + if ($p === false) continue; |
| 21 | + $c = file_get_contents($p); |
| 22 | + if ($c === false) continue; |
| 23 | + expect(preg_match('/\bstr_contains\s*\(/', $c))->toBe(0, "{$f} uses str_contains"); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('does not use str_starts_with (PHP 8.0)', function () use ($files) { |
| 28 | + foreach ($files as $f) { |
| 29 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 30 | + if ($p === false) continue; |
| 31 | + $c = file_get_contents($p); |
| 32 | + if ($c === false) continue; |
| 33 | + expect(preg_match('/\bstr_starts_with\s*\(/', $c))->toBe(0, "{$f} uses str_starts_with"); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not use str_ends_with (PHP 8.0)', function () use ($files) { |
| 38 | + foreach ($files as $f) { |
| 39 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 40 | + if ($p === false) continue; |
| 41 | + $c = file_get_contents($p); |
| 42 | + if ($c === false) continue; |
| 43 | + expect(preg_match('/\bstr_ends_with\s*\(/', $c))->toBe(0, "{$f} uses str_ends_with"); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not use nullsafe operator (PHP 8.0)', function () use ($files) { |
| 48 | + foreach ($files as $f) { |
| 49 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 50 | + if ($p === false) continue; |
| 51 | + $c = file_get_contents($p); |
| 52 | + if ($c === false) continue; |
| 53 | + expect(preg_match('/\?->/', $c))->toBe(0, "{$f} uses nullsafe operator"); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not use match expression (PHP 8.0)', function () use ($files) { |
| 58 | + foreach ($files as $f) { |
| 59 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 60 | + if ($p === false) continue; |
| 61 | + $c = file_get_contents($p); |
| 62 | + if ($c === false) continue; |
| 63 | + // Avoid false positive on preg_match etc |
| 64 | + $c2 = preg_replace('/preg_match|preg_match_all|fnmatch/', '', $c); |
| 65 | + expect(preg_match('/\bmatch\s*\(/', $c2))->toBe(0, "{$f} uses match expression"); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it('does not use union type declarations (PHP 8.0)', function () use ($files) { |
| 70 | + foreach ($files as $f) { |
| 71 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 72 | + if ($p === false) continue; |
| 73 | + $c = file_get_contents($p); |
| 74 | + if ($c === false) continue; |
| 75 | + // Match function params/return with union types like string|false |
| 76 | + $hits = preg_match_all('/function\s+\w+\s*\([^)]*\w+\s*\|\s*\w+/', $c); |
| 77 | + expect($hits)->toBe(0, "{$f} uses union types in function signatures"); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('does not use constructor property promotion (PHP 8.0)', function () use ($files) { |
| 82 | + foreach ($files as $f) { |
| 83 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 84 | + if ($p === false) continue; |
| 85 | + $c = file_get_contents($p); |
| 86 | + if ($c === false) continue; |
| 87 | + expect(preg_match('/function\s+__construct\s*\([^)]*\b(public|private|protected|readonly)\s/', $c))->toBe(0, |
| 88 | + "{$f} uses constructor promotion" |
| 89 | + ); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('uses array() not short syntax for new arrays', function () use ($files) { |
| 94 | + // This is a style preference for 1.2.x consistency, not a hard requirement |
| 95 | + // Just verify no mixed styles in the same file |
| 96 | + foreach ($files as $f) { |
| 97 | + $p = realpath(__DIR__ . '/../../' . $f); |
| 98 | + if ($p === false) continue; |
| 99 | + $c = file_get_contents($p); |
| 100 | + if ($c === false) continue; |
| 101 | + |
| 102 | + $hasArrayFunc = preg_match('/\barray\s*\(/', $c); |
| 103 | + $hasShortArray = preg_match('/=\s*\[/', $c); |
| 104 | + |
| 105 | + // Flag files that mix both styles |
| 106 | + if ($hasArrayFunc && $hasShortArray) { |
| 107 | + // Allow mixed if the file existed before our changes |
| 108 | + // This is informational, not a hard fail |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + expect(true)->toBeTrue(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments