Library for check dependency between modules inside projects

Overview

PHP Dependency analyzer

Build Status

PHP DA is tool for check and support dependencies inside your project clear.

For example:

  • You have project with 3 root namespaces: Domain, Application, Infrastructure
  • You want to be sure dependencies in your project defined as graph
[
    'dependencies' => [
        '\Domain' => null,
        '\Application' => ['\Domain'],
        '\Infrastructure' => ['\Domain', '\Application']
    ]
];

That means all classes from Domain namespace should use only classes from this namespace, and possibly vendor (it s configured).

All classes from Application can use classes from Domain and Application namespaces, but not from Infrastructure, etc

If some classes using dependencies not satisfied defined dependency graph, you give errors in report:

Have been analyzed 4 files
You have dependency problems in 2 files in your project:

Class \Application\TrackingService have errors:
    - Class \Application\TrackingService using class \Infrastructure\ShipImplementation which not satisfy dependency graph

Class \Domain\Cargo have errors:
    - Class \Domain\Cargo using class \Application\TrackingService which not satisfy dependency graph
    - Class \Domain\Cargo using class \Infrastructure\ShipImplementation which not satisfy dependency graph

Use cases

It can be useful in some cases for example:

  • You want to extract part of your application in separate service, you define valid dependencies and run php-da for investigate workload
  • You want to support low coupling in your application, you define valid dependencies and run php-da on your CI server for every MR, only for changed files
  • You want to make visible structure changes of your application for all developers, now it is visible in php-da config

Quick start

Install library using composer

composer require paglliac/dependency-analysis

Configuration

Add configuration file config.php to root of your project :

return [
    /**
     * REQUIRED
     * Dependencies Graph
     *
     * Description of valid dependencies in project
     *
     * This config means
     *
     * Package (every class in namespace) \Domain can use only classes from namespace \Domain or vendor dependencies
     * Package \Application can use only classes from namespaces \Domain, \Application or vendor dependencies
     * Package \Infrastructure can use only classes from namespaces \Domain, \Application, \Infrastructure or vendor dependencies
     */
    'dependencies' => [
        '\Domain' => null,
        '\Application' => ['\Domain'],
        '\Infrastructure' => ['\Domain', '\Application']
    ],

    /**
     * REQUIRED
     * Source path where dependencies will be analyzed
     */
    'path' => __DIR__,

    /**
     * OPTIONAL
     *
     * Make available to use vendor dependencies in whole project
     *
     * true - all project classes can use vendor dependencies
     * false - all project can not use vendor dependencies
     */
    'skip_vendor_dir' => true,

    /**
     * OPTIONAL
     * Flag that define how to do when some files placed in namespaces not presented in Dependencies Graph
     *
     * true - mark class as having incorrect dependencies
     * false - skip this file
     *
     * For example, in directory we have namespace \SomeNamespace with class \SomeNamespace\SomeClass
     * if flag is true, it will be marked as incorrect file, if flag is true, this file wil be marked as correct
     */
    'fail_on_non_presented_namespace' => false,

    /**
     * OPTIONAL
     * Flag for php parser, correct values:
     *
     * PhpParser\ParserFactory::PREFER_PHP7 - 1 (default)
     * PhpParser\ParserFactory::PREFER_PHP5 - 2
     * PhpParser\ParserFactory::ONLY_PHP7 - 3
     * PhpParser\ParserFactory::ONLY_PHP5 - 4
     */
    'php_version' => PhpParser\ParserFactory::PREFER_PHP7,

    /**
     * OPTIONAL
     * 
     * List of allowed files extensions, all files with other extensions will be skipped from analysis
     * 
     * Default - ['php']
     */
    'allowed_extensions' => ['php']
];

Running

Run dependency validation:

/vendor/bin/php-da -c config.php [files filter]

Options:

  • -c or --config is required option with the relative path to config file

Arguments:

  • [files filter] list of files for analysis, it's useful to use in CI combine with --diff

Example of output

Have been analyzed 4 files
You have dependency problems in 2 files in your project:

Class \Application\TrackingService have errors:
    - Class \Application\TrackingService using class \Infrastructure\ShipImplementation which not satisfy dependency graph

Class \Domain\Cargo have errors:
    - Class \Domain\Cargo using class \Application\TrackingService which not satisfy dependency graph
    - Class \Domain\Cargo using class \Infrastructure\ShipImplementation which not satisfy dependency graph

You might also like...
Magento-composer-installer - Composer installer for Magento modules
Magento-composer-installer - Composer installer for Magento modules

!!! support the maintainer of this project via Patreon: https://www.patreon.com/Flyingmana Magento Composer Installer The purpose of this project is t

This module adds a command to easily generate "modules" in Laravel and install them using composer.

Laravel Module Create This module adds a command to easily generate "modules" in Laravel and install them using composer Installation Simply install t

Friendly open source CMS forged on Codeigniter 3. FI v1.x uses the old lite theme and all modules.
Friendly open source CMS forged on Codeigniter 3. FI v1.x uses the old lite theme and all modules.

ForgeIgniter v1.x - CI-3.x Friendly open source CMS forged on Codeigniter 3 This version is now discontinued, please check version 2 or 3 for updates.

Scorm Player Api for play scorm modules

flux-scorm-player-api Scorm Player Api for play scorm modules Installation Native Download RUN (mkdir -p /%path%/libs/flux-scorm-player-api && cd /%pa

A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP.
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP.

A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP.

Detect flaws in your architecture, before they drag you down into the depths of dependency hell ...
Detect flaws in your architecture, before they drag you down into the depths of dependency hell ...

Detect flaws in your architecture before they drag you down into the depths of dependency hell ... What it does System Requirements Installation Phive

🪃 Zero-dependency global `kirbylog()` helper for any content

Kirbylog The most simple, Kirby-esque way to log content to file. Most of the time, I just want to log some string or array to a file. That's what thi

A PHP dependency vulnerabilities scanner based on the Security Advisories Database.

Enlightn Security Checker The Enlightn Security Checker is a command line tool that checks if your application uses dependencies with known security v

An installer package that let's you install NodeJS and NPM as a Composer dependency.

NodeJS installer for Composer This is an installer that will download NodeJS and NPM and install them in your Composer dependencies. Installation is s

Comments
  • Script fails due to unset output

    Script fails due to unset output

    Running the script with default config and getting an error:

    [15-Nov-2022 11:41:06 UTC] PHP Fatal error:  Uncaught Error: Typed property DependencyAnalysis\Config\Config::$output must not be accessed before initialization in /var/www/html/app/vendor/paglliac/dependency-analysis/src/Config/Config.php:90
    Stack trace:
    #0 /var/www/html/app/vendor/paglliac/dependency-analysis/src/Result/ResultPrinterFactory.php(13): DependencyAnalysis\Config\Config->getOutput()
    #1 /var/www/html/app/vendor/paglliac/dependency-analysis/src/AnalyzerFacade.php(21): DependencyAnalysis\Result\ResultPrinterFactory->make(Object(DependencyAnalysis\Config\Config))
    #2 /var/www/html/app/vendor/paglliac/dependency-analysis/bin/php-da(69): DependencyAnalysis\AnalyzerFacade->run('dependency-anal...', Array)
    #3 /var/www/html/app/vendor/bin/php-da(115): include('/var/www/html/a...')
    #4 Command line code(1): require('/var/www/html/a...')
    #5 {main}
      thrown in /var/www/html/app/vendor/paglliac/dependency-analysis/src/Config/Config.php on line 90
    
    1. Would be nice to have a description of all possible config parameters.
    2. output is not set by default, but indeed accessed.
    opened by ilya-levin-lokalise 0
  • Script cannot parse a file

    Script cannot parse a file

    Script fails to parse the following file:

    <?php
    
    namespace App\Controller;
    
    /**
     * @Route(path="/sso")
     */
    class SSOController extends BaseController
    {
        /**
         * @Route(path="/{domain}", methods={"GET"})
         */
        public function loginForm(Request $request, string $domain): Response
        {
    
        }
    }
    

    With an error:

    [15-Nov-2022 11:34:34 UTC] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to DependencyAnalysis\Parser\FileParser::processName() must be an instance of PhpParser\Node\Name, instance of PhpParser\Node\Identifier given, called in /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php on line 151 and defined in /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php:266
    Stack trace:
    #0 /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php(151): DependencyAnalysis\Parser\FileParser->processName(Object(PhpParser\Node\Identifier))
    #1 /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php(111): DependencyAnalysis\Parser\FileParser->processStmts(Array)
    #2 /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php(92): DependencyAnalysis\Parser\FileParser->processStmts(Array)
    #3 /var/www/html/app/vendor/paglliac/dependency-analysis/src/Analyzer.php(22): DependencyAnalysis\Parser\FileParser->parseFile('/var/www/html/a...')
    #4 /var/www/ in /var/www/html/app/vendor/paglliac/dependency-analysis/src/Parser/FileParser.php on line 96
    

    If we look at \PhpParser\Node\Param, indeed, $type can have different types:

        /** @var null|Identifier|Name|ComplexType Type declaration */
        public $type;
    
    opened by ilya-levin-lokalise 0
  • Can't run the script due to Composer autoload path issues

    Can't run the script due to Composer autoload path issues

    Steps to reproduce:

    1. composer require --dev paglliac/dependency-analysis
    2. vendor/bin/php-da

    Expected: script runs Actual:

    You need to set up the project dependencies using Composer:
    
        composer install
    
    You can learn all about Composer on https://getcomposer.org/.
    

    I'm not entirely sure what causes the issue, but it seems this code: image

    Problem is, the executable is located in vendor/paglliac/dependency-analysis/bin/php-da, so basically, none of the links hits the actual autoload.php.

    opened by ilya-levin-lokalise 0
Releases(v0.1.2)
Owner
Kir Apukhtin
Kir Apukhtin
A small CLI tool to check missing dependency declarations in the composer.json and module.xml

Integrity checker Package allows to run static analysis on Magento 2 Module Packages to provide an integrity check of package. Supported tools: Compos

run_as_root GmbH 13 Dec 19, 2022
This tool check syntax of PHP files faster than serial check with fancier output.

PHP Parallel Lint This application checks syntax of PHP files in parallel. It can output in plain text, colored text, json and checksyntax formats. Ad

PHP Parallel lint 202 Dec 22, 2022
PHP Parallel Lint - This tool check syntax of PHP files faster than serial check with fancier output

PHP Parallel Lint This application checks syntax of PHP files in parallel. It can output in plain text, colored text, json and checksyntax formats. Ad

PHP Parallel lint 156 Apr 24, 2022
This composer plugin allows you to share your selected packages between your projects by creating symlinks

Composer - Shared Package Plugin This composer plugin allows you to share your selected packages between your projects by creating symlinks. All share

L'Etudiant 169 Sep 20, 2022
A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc). Based on the difflib implementation in Python

PHP Diff Class Introduction A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences

Chris Boulton 708 Dec 25, 2022
A tool that can be used to verify BC breaks between two versions of a PHP library.

Roave Backward Compatibility Check A tool that can be used to verify BC breaks between two versions of a PHP library. Pre-requisites/assumptions Your

Roave, LLC 530 Dec 27, 2022
Test essentials for writing testable code that interacts with Magento core modules

Essentials for testing Magento 2 modules Using mocking frameworks for testing Magento 2 modules is counterproductive as you replicate line by line you

EcomDev B.V. 9 Oct 6, 2022
Experimenting with Zabbix frontend modules

Modules Request for new example can be left as issue for this repository, will do my best to implement if it is possible. Extending host popup menu Ac

null 1 Sep 19, 2022
Simple way to auto load modules without complicate definition of routes

Phalcon-autorouter Version 2.0 Example full application with various libraries included in this example Change log Structure have been changed and upd

Kamil 28 Dec 27, 2022
WordPress plugin which contains a collection of modules to apply theme-agnostic front-end modifications

Soil A WordPress plugin which contains a collection of modules to apply theme-agnostic front-end modifications. Soil is a commercial plugin available

Roots 1k Dec 20, 2022