PHP Namespace Inspector
VS Code extension to inspect and detect classes with incorrect namespace according to file path based on autoload configuration in composer.json.
Features
- ✅ Automatically read autoload configuration from
composer.json (PSR-4, PSR-0, classmap)
- ✅ Automatically detect namespaces declared in autoload configuration
- ✅ Automatic project base path detection - Finds project root by locating
composer.json
- ✅ Monorepo support - Works with multiple projects in a single workspace
- ✅ Support include/exclude folders in settings
- ✅ Display report in Output Channel with issue count
- ✅ Status bar displays issue count
- ✅ Can enable/disable extension via settings
- ✅ Automatically scan when opening workspace (can be disabled)
- ✅ Skip files without namespace declaration (ignores classes without namespace)
- ✅ PSR-4 violation detection - Detects namespaces that incorrectly include class name
Installation
- Open VS Code
- Press
F5 to run extension in Development Host
- Or package extension and install from
.vsix file
Usage
Manual Scan
- Open Command Palette (
Ctrl+Shift+P or Cmd+Shift+P)
- Select command:
PHP Namespace Inspector: Scan PHP Namespaces
Auto Scan
Extension will automatically scan when opening workspace (if autoScanOnOpen is enabled).
View Results
After scanning, results will be displayed in:
- Output Channel: Tab "PHP Namespace Inspector" in Output panel
- Status Bar: Issue count at bottom right corner
Configuration
Open Settings (Ctrl+, or Cmd+,) and search for PHP Namespace Inspector:
phpNamespaceInspector.enabled
- Default:
true
- Description: Enable/disable extension
phpNamespaceInspector.includeFolders
- Default:
[] (scan all according to composer autoload)
- Description: List of folders to scan. Empty will scan all according to autoload configuration in composer.json
phpNamespaceInspector.excludeFolders
- Default:
["vendor", "node_modules", ".git"]
- Description: List of folders to exclude when scanning
phpNamespaceInspector.autoScanOnOpen
- Default:
true
- Description: Automatically scan when opening workspace
Configuration Example
{
"phpNamespaceInspector.enabled": true,
"phpNamespaceInspector.includeFolders": ["src", "app"],
"phpNamespaceInspector.excludeFolders": ["vendor", "tests", "node_modules"],
"phpNamespaceInspector.autoScanOnOpen": true
}
Requirements
- VS Code >= 1.74.0
- Workspace must have
composer.json file with autoload configuration
How It Works
- Extension automatically finds project base path by locating
composer.json file
- Reads
composer.json file from detected project root
- Parses autoload configuration (PSR-4, PSR-0, classmap)
- Scans all
.php files in autoloaded directories
- Automatically detects if namespaces are declared in autoload configuration
- Compares namespace in file with expected namespace based on file path
- Detects PSR-4 violations (namespace ending with class name)
- Skips files without namespace declaration
- Displays list of files with mismatched namespace
Detection Logic
The extension detects the following issues:
- Namespace mismatch: File has namespace but doesn't match expected namespace based on file path
- PSR-4 violation: Namespace incorrectly includes the class name at the end (e.g.,
namespace App\Services\PaymentService; for class PaymentService)
- Namespace not in autoload: File has namespace but it's not declared in autoload configuration
- File location mismatch: File has namespace declared in autoload but is not in the expected directory
- Files without namespace: Automatically skipped (not reported as issues)
Automatic Base Path Detection
The extension automatically searches for composer.json to determine the project root:
- First checks workspace root directory
- If not found, searches subdirectories (up to 5 levels deep)
- Skips common directories like
node_modules, .git, vendor
- Displays detected base path in output channel
- Supports monorepo structures with multiple projects
Development
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Watch mode
npm run watch
License
MIT