
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
68 lines
2.4 KiB
Plaintext
68 lines
2.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Namespaces should be preferred over the `include` or `require` functions in PHP because they provide a cleaner and more organized approach for managing code dependencies.
|
|
Namespaces allow you to logically group related classes, functions, and constants, preventing naming conflicts and improving code readability.
|
|
They also promote code reusability by enabling modularization and encapsulation.
|
|
On the other hand, using `include` or `require` functions directly can lead to a cluttered global namespace and make it harder to track and manage dependencies, especially in larger projects.
|
|
|
|
=== Exceptions
|
|
|
|
This rule doesn't raise issues on ``++autoload.php++`` files.
|
|
This file is typically generated by Composer that provides an autoloading mechanism for classes and functions. Composer is a dependency management tool for PHP that simplifies the process of integrating external libraries and packages into your project.
|
|
|
|
== How to fix it
|
|
|
|
It is recommended to use the PSR-4 autoloading standard and to use Composer to manage the loading of classes and functions.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,php,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
require_once './shop/modules/vegetable/src/entity/Tomato.php';
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,php,diff-id=1,diff-type=compliant]
|
|
----
|
|
use Shop\Vegetable\Tomato
|
|
----
|
|
|
|
=== How does this work?
|
|
To import classes and functions with namespaces using Composer in PHP you have to ensure that the project is set up to use Composer and have a valid composer.json file in the root directory. Make sure the namespace corresponds to the directory structure of your classes.
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* https://www.php-fig.org/psr/psr-4/[PSR-4: Autoloader]
|
|
* https://getcomposer.org/doc/00-intro.md[Composer - PHP Dependency Management]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Replace [include|include_once|require|require_once] with namespace import mechanism through the "use" keyword.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 14 Oct 2018, 11:18:58 Pierre-Loup Tristant wrote:
|
|
There is no "Sonar way recommended" quality profile in SonarPHP.
|
|
|
|
Should I put the rule in "Sonar way" ?
|
|
|
|
=== on 14 Oct 2018, 11:19:04 Pierre-Loup Tristant wrote:
|
|
Pull request :
|
|
|
|
https://github.com/SonarSource/sonar-php/pull/362
|
|
|
|
endif::env-github,rspecator-view[]
|