|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Analyzer |
| 4 | +permalink: /docs/analyzer/ |
| 5 | +--- |
| 6 | + |
| 7 | +The "Analyzer" class checks PHP files for disallowed output functions to ensure proper separation between logic (actions) and presentation (views), maintaining clean MVC architecture. |
| 8 | + |
| 9 | +## Execute |
| 10 | + |
| 11 | +``` |
| 12 | +Analyzer::execute(): void |
| 13 | +``` |
| 14 | + |
| 15 | +Execute the analyzer to check all action and view files in the current request. This is automatically called by the framework when debug mode is enabled. |
| 16 | + |
| 17 | +The analyzer checks: |
| 18 | +- Template actions |
| 19 | +- Page actions |
| 20 | +- Views |
| 21 | +- Template views |
| 22 | + |
| 23 | +For disallowed functions that could break the MVC pattern. |
| 24 | + |
| 25 | +Example: |
| 26 | + |
| 27 | +``` |
| 28 | +if (Debugger::$enabled) { |
| 29 | + Analyzer::execute(); |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## What It Checks |
| 34 | + |
| 35 | +The Analyzer looks for these disallowed functions in your code: |
| 36 | + |
| 37 | +### In Actions (*.php files) |
| 38 | + |
| 39 | +Actions should not directly output content. These functions are not allowed: |
| 40 | +- `echo` - Use variables instead |
| 41 | +- `print` - Use variables instead |
| 42 | +- `exit` - Use `Router::redirect()` instead |
| 43 | +- `die` - Use `Router::redirect()` instead |
| 44 | +- `var_dump` - Use `d()` function instead |
| 45 | +- `eval` - Security risk, avoid entirely |
| 46 | + |
| 47 | +### In Views (*.phtml files) |
| 48 | + |
| 49 | +Views should use the `e()` function for safe output. These are not allowed: |
| 50 | +- `echo` - Use `e()` function instead |
| 51 | +- `print` - Use `e()` function instead |
| 52 | +- `<?= ?>` short tags - Use `<?php e($var); ?>` instead |
| 53 | +- `exit`, `die` - Should not be in views |
| 54 | +- `var_dump` - Use `d()` function instead |
| 55 | +- `eval` - Security risk, avoid entirely |
| 56 | + |
| 57 | +## Why This Matters |
| 58 | + |
| 59 | +The analyzer enforces MintyPHP's clean separation of concerns: |
| 60 | + |
| 61 | +**Actions (*.php)** handle logic: |
| 62 | +```php |
| 63 | +<?php |
| 64 | +// Good: Store data in variables |
| 65 | +$users = DB::select('SELECT * FROM users'); |
| 66 | +$total = count($users); |
| 67 | + |
| 68 | +// Bad: Direct output in action |
| 69 | +echo "Total users: " . $total; // Will trigger warning |
| 70 | +``` |
| 71 | + |
| 72 | +**Views (*.phtml)** handle presentation: |
| 73 | +```php |
| 74 | +<?php |
| 75 | +// Good: Use e() function for safe output |
| 76 | +<h1>Users</h1> |
| 77 | +<p>Total: <?php e($total); ?></p> |
| 78 | + |
| 79 | +// Bad: Use of echo |
| 80 | +<p>Total: <?php echo $total; ?></p> // Will trigger warning |
| 81 | +``` |
| 82 | + |
| 83 | +## Example Warnings |
| 84 | + |
| 85 | +When the analyzer detects violations, it triggers warnings like: |
| 86 | + |
| 87 | +``` |
| 88 | +Warning: MintyPHP action "pages/users/list().php" should not use "echo". Error raised in... |
| 89 | +``` |
| 90 | + |
| 91 | +``` |
| 92 | +Warning: MintyPHP view "pages/users/list(default).phtml" should not use "echo". Error raised in... |
| 93 | +``` |
| 94 | + |
| 95 | +## Best Practices |
| 96 | + |
| 97 | +### In Actions |
| 98 | + |
| 99 | +✅ **Good:** |
| 100 | +```php |
| 101 | +<?php |
| 102 | +use MintyPHP\DB; |
| 103 | +use MintyPHP\Router; |
| 104 | + |
| 105 | +// Fetch data |
| 106 | +$users = DB::select('SELECT * FROM users'); |
| 107 | + |
| 108 | +// Process data |
| 109 | +$activeUsers = array_filter($users, fn($u) => $u['active']); |
| 110 | + |
| 111 | +// Handle redirects properly |
| 112 | +if (empty($activeUsers)) { |
| 113 | + Router::redirect('error/not_found'); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +❌ **Bad:** |
| 118 | +```php |
| 119 | +<?php |
| 120 | +// Don't output directly |
| 121 | +echo "<h1>Users</h1>"; |
| 122 | +print_r($users); |
| 123 | + |
| 124 | +// Don't use exit/die |
| 125 | +if (empty($users)) { |
| 126 | + die('No users found'); |
| 127 | +} |
| 128 | + |
| 129 | +// Don't use var_dump |
| 130 | +var_dump($users); |
| 131 | +``` |
| 132 | + |
| 133 | +### In Views |
| 134 | + |
| 135 | +✅ **Good:** |
| 136 | +```php |
| 137 | +<?php |
| 138 | +use MintyPHP\Session; |
| 139 | + |
| 140 | +<form method="post"> |
| 141 | + <input name="username" /> |
| 142 | + <button type="submit">Login</button> |
| 143 | + <?php Session::getCsrfInput(); ?> |
| 144 | +</form> |
| 145 | + |
| 146 | +<?php foreach ($users as $user): ?> |
| 147 | + <p><?php e($user['name']); ?></p> |
| 148 | +<?php endforeach; ?> |
| 149 | +``` |
| 150 | + |
| 151 | +❌ **Bad:** |
| 152 | +```php |
| 153 | +<!-- Don't use short echo tags --> |
| 154 | +<p><?= $user['name'] ?></p> |
| 155 | + |
| 156 | +<!-- Don't use echo directly --> |
| 157 | +<p><?php echo $user['name']; ?></p> |
| 158 | + |
| 159 | +<!-- Don't use print --> |
| 160 | +<?php print $message; ?> |
| 161 | +``` |
| 162 | + |
| 163 | +## Debugging Output |
| 164 | + |
| 165 | +Instead of `var_dump()` or `print_r()`, use the `d()` function which integrates with the debugger: |
| 166 | + |
| 167 | +```php |
| 168 | +<?php |
| 169 | +// In actions |
| 170 | +$user = DB::selectOne('SELECT * FROM users WHERE id = ?', $userId); |
| 171 | +d($user); // Outputs to debugger panel, not page |
| 172 | + |
| 173 | +// Multiple values |
| 174 | +d($user, $permissions, $settings); |
| 175 | +``` |
| 176 | + |
| 177 | +The `d()` function: |
| 178 | +- Only works when debugger is enabled |
| 179 | +- Logs to the "Logging" panel in the debugger |
| 180 | +- Includes file and line number information |
| 181 | +- Limits output to prevent memory issues |
| 182 | + |
| 183 | +## Configuration |
| 184 | + |
| 185 | +The Analyzer runs automatically when the debugger is enabled. Enable it in `config/config.php`: |
| 186 | + |
| 187 | +```php |
| 188 | +use MintyPHP\Debugger; |
| 189 | + |
| 190 | +Debugger::$enabled = true; // Analyzer runs automatically |
| 191 | +``` |
| 192 | + |
| 193 | +## When It Runs |
| 194 | + |
| 195 | +The Analyzer is executed automatically: |
| 196 | +1. After routing is complete |
| 197 | +2. Before actions and views are loaded |
| 198 | +3. Only when `Debugger::$enabled` is true |
| 199 | + |
| 200 | +This means violations are caught during development but don't affect production performance. |
| 201 | + |
0 commit comments