PHP Classes

How to Generate a PHP Dependency Graph to Determine the Stability of a Project Using the Tool PHP Class Dependencies Analyzer: Analyze the dependencies of project classes

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-11-03 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
php-class-dependenci 1.0The PHP License8Tools, Language, PHP 8
Description 

Author

This package can analyze the dependencies of project classes.

It provides a command-line console tool to run from the to check the class files in a given directory.

The tool can generate a report in the form of a graph image that shows the dependencies of the classes that were found.

Picture of DeGraciaMathieu
  Performance   Level  
Innovation award
Innovation award
Nominee: 16x

Winner: 2x

 

Documentation

<p align="center"> <img src="https://github.com/DeGraciaMathieu/php-smelly-code-detector/blob/master/arts/robot.png" width="250"> </p>

php-class-dependencies-analyzer

testing

> ?? For French developers, an article on Laravel France explores these stable dependency concepts.

Phar

This tool is distributed as a PHP Archive (PHAR):

wget https://github.com/DeGraciaMathieu/php-class-dependencies-analyzer/raw/main/builds/class-dependencies-analyzer

php class-dependencies-analyzer --version

Why

Inevitably, in an object-oriented project, classes will depend on each other.

These dependencies generate instability that can be measured with the following formula:

Instability = Efferent / (Afferent + Efferent)

Afferent coupling represents the number of components that depend on a given component, while efferent coupling counts the number of dependencies a given component has.

<img src='https://cdn.laravel-france.com/images/pictures/6aa50c00-414c-4817-928d-c67d1bf996e0.png'>

Instability ranges between 0 and 1, where 0 represents a fully stable class and 1 represents a fully unstable class.

Stable class has few dependencies but is depended on by many components. Therefore, a stable class is critical to the project and must be reliable and well-tested.

An unstable class has many dependencies but few components depend on it. Therefore, it?s easier to modify, but it?s more likely to be affected by changes in its dependencies. It is recommended to monitor the dependencies of an unstable component and replace those that are even more unstable with an abstraction.

It?s important to manage these dependencies between classes to maintain maintainable, modular, and easily testable code.

This tool allows you to:

  • Detect classes with a high coupling instability rate
  • Detect cyclic dependencies among a set of classes
  • Detect fragile dependencies between classes

Visualizing Instability

To visualize the instability and coupling of your classes:

php class-dependencies-analyzer analyze ./path/to/project

?? To obtain accurate results, it is important to analyze your entire codebase, use the --only=* or --exclude=* options to filter the results.

php class-dependencies-analyzer analyze app --only=App\\Domain,Infrastructure
php class-dependencies-analyzer analyze app --exclude=Models

For visualizing the results in a graph, you can use the --graph option:

php class-dependencies-analyzer analyze app --only=App\\Domain --graph

<img style="width: 50%;" src='/arts/graph.png'>

Detecting Cyclic Dependencies

A dependency cycle is a class that depends on itself through its dependencies.

<img src='https://cdn.laravel-france.com/images/pictures/34ddeac2-d9ac-4ebb-a54c-48f7940945ca.png'>

This violates the acyclic dependencies principle, it can be a sign of a bad design and reveal an ensemble of components difficult to maintain and evolve.

php class-dependencies-analyzer cyclic ./path/to/project

> You can filter the results using the --only=* or --exclude=* options.

Detecting Fragile Dependencies

A fragile dependancy is a class that depends on a class that is more unstable than it.

It can be a sign of a bad design and an indicator of a class that can suffer from side effects of its dependencies.

The first class will be sensitive to any changes made to the second class and will experience side effects.

<img src='https://cdn.laravel-france.com/images/pictures/c80b59f3-ffad-4609-9364-f8efa4e62c9a.png'>

These dependencies can generate bugs and difficulties in evolution.

php class-dependencies-analyzer weakness ./path/to/project

To filter results based on an instability delta, you can use the --min-delta option.

php class-dependencies-analyzer weakness ./path/to/project --min-delta=0.1

The delta corresponds to the difference in stability between a class and one of its dependencies. The greater this difference, the more likely the first component is to suffer from side effects caused by its unstable dependency.

> You can filter the results using the --only=* or --exclude=* options.

> [!TIP] > Other analysis tools are available.


  Files folder image Files (112)  
File Role Description
Files folder image.github (1 directory)
Files folder imageapp (5 directories)
Files folder imagearts (1 file)
Files folder imagebootstrap (2 files)
Files folder imageconfig (3 files)
Files folder imageresources (1 directory)
Files folder imagetests (3 files, 3 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file box.json Data Auxiliary data
Plain text file class-dependencies-analyzer Class Class source
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0