Skip to content
| Marketplace
Sign in
Visual Studio Code>Linters>PHP Namespace InspectorNew to Visual Studio Code? Get it now.
PHP Namespace Inspector

PHP Namespace Inspector

NoTeNo

|
5 installs
| (0) | Free
Inspect and detect PHP classes with incorrect namespace according to file path based on autoload configuration in composer.json. Automatically detects namespaces declared in autoload and validates namespace correctness.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

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

  1. Open VS Code
  2. Press F5 to run extension in Development Host
  3. 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

  1. Extension automatically finds project base path by locating composer.json file
  2. Reads composer.json file from detected project root
  3. Parses autoload configuration (PSR-4, PSR-0, classmap)
  4. Scans all .php files in autoloaded directories
  5. Automatically detects if namespaces are declared in autoload configuration
  6. Compares namespace in file with expected namespace based on file path
  7. Detects PSR-4 violations (namespace ending with class name)
  8. Skips files without namespace declaration
  9. 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

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft