PHPackages                             kajstrom/dependency-constraints - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kajstrom/dependency-constraints

ActiveLibrary

kajstrom/dependency-constraints
===============================

Create constraints in your test suite to prevent unwanted dependencies.

1.0.1(8y ago)236BSD-3-ClausePHPPHP ^7.1.0

Since Jan 19Pushed 8y ago1 watchersCompare

[ Source](https://github.com/kajstrom/dependency-constraints)[ Packagist](https://packagist.org/packages/kajstrom/dependency-constraints)[ RSS](/packages/kajstrom-dependency-constraints/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (11)Used By (0)

[![Tracis CI](https://camo.githubusercontent.com/1964896fe9c59b8d50e86fd4df7a2f3e20d9508bcb8a2945e36a742cf80e20eb/68747470733a2f2f7472617669732d63692e6f72672f6b616a7374726f6d2f646570656e64656e63792d636f6e73747261696e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kajstrom/dependency-constraints)[![Version](https://camo.githubusercontent.com/a10620e9922347f70fdc0cae6a2ecf82fbe03489314c074db6e45cfd3a469881/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b616a7374726f6d2f646570656e64656e63792d636f6e73747261696e74732e737667)](https://packagist.org/packages/kajstrom/dependency-constraints)[![PHP version](https://camo.githubusercontent.com/95c69191547b9f9e3879535432d2d9de1ea1fef0c999c9f4ad538ee1a36b4f12/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6b616a7374726f6d2f646570656e64656e63792d636f6e73747261696e74732e737667)](https://packagist.org/packages/kajstrom/dependency-constraints)

DependencyConstraints
=====================

[](#dependencyconstraints)

DependencyConstraints is a static code analysis tool for creating constraints for dependencies between modules in your project. It is intended to be used within a testing library such as PHPUnit or Codeception.

The core idea of DependencyConstraints is to shorten the feedback loop for finding changes that degrade the architecture of your application. Instead of finding out 6 months later that you or someone else on the team added undesirable coupling into your application, you can create tests that act as a safeguard against such changes.

DependencyConstraints is inspired by JDepend and Fitness Functions introduced in the book [Building Evolutionary Architectures](https://www.thoughtworks.com/books/building-evolutionary-architectures).

Getting started
---------------

[](#getting-started)

To get started require DependencyConstraints with Composer.

```
composer require kajstrom/dependency-constraints --dev

```

After DependencyConstraints has been installed you can start creating tests with it. It is recommended to create only a single instance of DependencyConstraints as it will go through all PHP files in your in the target directory.

In addition, you should use DependencyConstraints on your own src/classes directory. It is not necessary for DependencyConstraints to parse vendor directory etc. to be able to look for usages of external classes.

Using PHPUnit the test would look like this.

```
    class MyDependencyTest extends TestCase
    {
        /** @var  DependencyConstraints */
        private static $dc;

        public static function setUpBeforeClass()
        {
            self::$dc = new DependencyConstraints("/path/to/myproject/src");
        }

        public function testModuleAIsNotDependentOnModuleX()
        {
            $moduleA = self::$dc->getModule("MyProject\\ModuleA");

            $this->assertFalse(
                $moduleA->dependsOnModule("MyProject\\ModuleX"),
                $moduleA->describeDependenciesTo("MyProject\\ModuleX"),
            );
        }
    }
```

What is considered to be a dependency
-------------------------------------

[](#what-is-considered-to-be-a-dependency)

DependencyConstraints assumes the following to be dependencies:

- Using a class, interface, trait or instances of a class from another module (namespace).
- Using a function from another module.
- Using a constant from another module.

Currently globally scoped classes, functions and constants are not considered to be dependencies.

Using something from a submodule is not considered to be a dependency.

For example:

Using "MyProject\\ModuleA\\SomeClass" in "MyProject\\ModuleB" is a dependency.

Using "MyProject\\ModuleA\\SubModule\\SomeClass" in "MyProject\\ModuleA" is not a dependency.

Potential use cases
-------------------

[](#potential-use-cases)

In a layered architecture you might want to prevent other layers of your software from becoming coupled to the presentation layer.

```
public function testBusinessLayerDoesNotDependOnPresentationLayer()
{
    $dc = new DependencyConstraints("path/to/my/src");
    $business = $dc->getModule("MyProject\\Business");

    $this->assertFalse(
        $business->dependsOnModule("MyProject\\Presentation"),
        $business->describeDependenciesTo("MyProject\\Presentation")
    );
}
```

Perhaps you have a modular monolith and want to ensure that a certain module will not get coupled to another module.

```
public function testModuleADoesNotDependUponModuleB()
{
    $dc = new DependencyConstraints("path/to/my/src");
    $moduleA = $dc->getModule("MyProject\\ModuleA");

    $this->assertFalse(
        $moduleA->dependsOnModule("MyProject\\ModuleB"),
        $moduleA->describeDependenciesTo("MyProject\\ModuleB")
    );
}
```

Or maybe you want to keep certain external libraries out of the Application layer in a Hexagonal Architecture.

```
public function testApplicationLayerDoesNotDependOnSymfonyHttpFoundation()
{
    $dc = new DependencyConstraints("path/to/my/src");
    $application = $dc->getModule("MyProject\Application");

    $this->assertFalse(
        $application->dependsOnModule("Symfony\\HttpFoundation"),
        $application->describeDependenciesTo("Symfony\\HttpFoundation")
    );
}
```

You can also check for dependencies on a certain class. Maybe you are refactoring it out but it keeps popping up in new places all the time!

```
public function testModuleDoesNotDependOnSingleton()
{
    $dc = new DependencyConstraints("path/to/my/src");
    $module = $dc->getModule("MyProject\\Module");

    $this->assertFalse($module->hasDependencyOn("MyProject\\Utils\\SingletonThatSeemedAGoodIdeaBackThen");
}
```

Limitations
-----------

[](#limitations)

As a static code analysis tool DependencyConstraints can't catch absolutely everything. Let's say someone wants to be clever and do something like this:

```
$className = "MyProject\\Module\\Class";
$instance = new $className;
```

This would not be found as a dependency when analyzing the source.

Why not use Pdepend?
--------------------

[](#why-not-use-pdepend)

Pdepend offers very useful metrics on the quality of the codebase, but it does not allow testing dependencies between modules.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

10

Last Release

2978d ago

Major Versions

0.0.0-beta3 → 1.0.02018-03-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/b655c14ae3221d88365cec6107c3a00fbdf9e8d1a919333f31de386272f12ac5?d=identicon)[kajstrom](/maintainers/kajstrom)

---

Top Contributors

[![kajstrom](https://avatars.githubusercontent.com/u/1039316?v=4)](https://github.com/kajstrom "kajstrom (80 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/kajstrom-dependency-constraints/health.svg)

```
[![Health](https://phpackages.com/badges/kajstrom-dependency-constraints/health.svg)](https://phpackages.com/packages/kajstrom-dependency-constraints)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
