Class File Include

ID

php.class_includes

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Php

Tags

code_smell, maintainability

Description

Reports include/require statements that load a single class file by a literal path, for example require_once 'src/MyClass.php';. Class files should be loaded through PSR-4 / Composer autoloading rather than included manually.

Rationale

Manually including class files hard-codes file-system paths and forces the code to know the exact location and load order of every class it uses. This is brittle: moving or renaming a file breaks the include, and the same class can be required twice from different paths. Autoloading resolves a class name to a file on demand, so adding, moving or renaming classes needs no edits to call sites, and each class is loaded exactly once. The rule looks only at literal paths that resemble class files — a PascalCase basename ending in .php, a path under a classes/ directory, or a legacy class.Name.php basename — and leaves configuration, template and helper includes untouched.

<?php
require_once 'src/MyClass.php';   // FLAW — class file pulled in by literal path
include 'app/classes/helpers.php';// FLAW — file under a classes/ directory

require_once 'config/settings.php'; // OK — configuration, not a class
include $templatePath;              // OK — variable path, not inspected

Remediation

Register the class with a PSR-4 autoloader (typically via Composer’s autoload section) and remove the manual include. Reference the class by its fully qualified name and let the autoloader resolve the file.