Skip to content

PHP: a class whose name is a prefix of sibling class names loses all inbound imports edges #2659

Closed

Description

@alejandro-vargas-m

Version: graphify 0.9.28 · Python 3.10 · macOS · --code-only

A class whose name is a strict prefix of other class names in the same project loses all of its inbound imports edges. affected then reports No affected nodes found for a class that is imported by several files.

This looks related to the namespace/use-aware PHP resolution pass added in 0.9.17 for #1923.

Minimal reproduction

Five files, graphify extract . --code-only --force.

Case A — works. Pivot plus two importers:

// src/Pivot.php
<?php
namespace App\Entities;
class Pivot { public $id; }
// src/ModelWithRelation.php
<?php
namespace App\Models;
use App\Entities\Pivot;
class ModelWithRelation
{
    public function rows() { return $this->hasMany(Pivot::class, 'model_id'); }
}
// src/ServiceWithTypeHint.php
<?php
namespace App\Services;
use App\Entities\Pivot;
class ServiceWithTypeHint
{
    public function __construct(Pivot $pivot) { $this->pivot = $pivot; }
}
$ graphify affected "Pivot" --depth 2
- ModelWithRelation.php [imports] src/ModelWithRelation.php:L4
- ServiceWithTypeHint.php [imports] src/ServiceWithTypeHint.php:L4
- .__construct() [references] src/ServiceWithTypeHint.php:L9

$ graphify explain "Pivot"
  Degree: 4

Case B — broken. Same Pivot, same importer, but add three siblings whose names begin with Pivot:

// src/PivotRepository.php
<?php
namespace App\Repositories;
interface PivotRepository {}
// src/PivotRepositoryEloquent.php
<?php
namespace App\Repositories;
use App\Entities\Pivot;
class PivotRepositoryEloquent implements PivotRepository
{
    public function model() { return Pivot::class; }
}
// src/PivotValidator.php
<?php
namespace App\Validators;
class PivotValidator {}
// src/ModelWithRelation.php   (unchanged)
<?php
namespace App\Models;
use App\Entities\Pivot;
class ModelWithRelation
{
    public function rows() { return $this->hasMany(Pivot::class, 'model_id'); }
}
$ graphify affected "Pivot" --depth 2
No affected nodes found.

$ graphify explain "Pivot"
  Degree: 1
  <-- Pivot.php [contains] src/Pivot.php:L3

Both use App\Entities\Pivot; statements are gone from the graph. The only surviving edge is the file's own contains. Adding the prefix-siblings is the single change between A and B.

Real-world impact

Hit this on a Laravel 8 codebase (~4.4k nodes). App\Entities\ConciliacionesBanorteFacturas is imported by four files, and coexists with ConciliacionesBanorteFacturasRepository, ConciliacionesBanorteFacturasRepositoryEloquent and ConciliacionesBanorteFacturasValidator — the exact shape above, which prettus/l5-repository generates for every entity, so any Laravel project using that package is affected across its whole data layer.

The importing file still emits its other imports, so the loss is silent and per-target:

app_models_conciliacionbanorte --imports--> app_entities_cliente               ✅
app_models_conciliacionbanorte --imports--> app_entities_complementospagos     ✅
app_models_conciliacionbanorte --imports--> app_models_bkcomplementopago       ✅
                              (no edge)     app_entities_conciliacionesbanortefacturas   ❌

The practical danger is that affected returns an empty result rather than an error, which reads as "nothing depends on this" when the honest answer is "unknown" — the failure mode most likely to green-light a bad refactor. It also under-ranks these classes in god-nodes.

Secondary observation

Independently of the above, in Case A the method .rows() never appears: a class referenced as X::class inside a method body produces no method-level references edge, while the same class in a constructor signature does. Only the file-level import survives.

That pattern is how every Eloquent relationship is declared — hasMany, belongsTo, morphMany, morphTo — so in ORM-heavy code the densest part of the real dependency graph is the least represented. Happy to split this into its own issue if you prefer.

Expected

  • A use import always produces an edge, regardless of whether other classes share its name as a prefix.
  • If resolution genuinely cannot determine a target, that is reported rather than dropped silently.

Glad to test a patch — the project above is a decent stress case at ~4.4k nodes with heavy Eloquent and prettus/l5-repository usage.

Activity

  1. safishamsi commented on Aug 12, 2026

    @safishamsi
    Member

    Fixed in v0.9.41 (graphifyy==0.9.41 on PyPI). A PHP use import written with a leading backslash / fully-qualified prefix now resolves to its target definition (and does not mis-bind to a same-prefixed sibling class). Thanks @ousamabenyounes (PR #2661).

  2. alejandro-vargas-m commented on Aug 12, 2026

    @alejandro-vargas-m
    Author

    Thanks for the quick turnaround — confirmed the prefix-sibling fix works. Verified 0.9.41 against the real project (~4.4k nodes): affected "ConciliacionesBanorteFacturas" went from No affected nodes found to correctly listing the files that import it. It also works for plain use App\Entities\Pivot; without a leading backslash, which is a bit broader than the closing note suggested.

    The secondary observation from the original report is still open, and having now measured it I think it's worth its own tracking — happy to file separately if you'd rather, just say the word.

    A class referenced as X::class inside a method body produces no edge when the file has no use for it.

    Two files, graphify extract . --code-only --force on 0.9.41:

    // src/Pivot.php
    <?php
    namespace App\Entities;
    class Pivot { public $id; }
    // src/SinUse.php  — same namespace, so no `use` is needed or possible to require
    <?php
    namespace App\Entities;
    class SinUse
    {
        public function rows() { return $this->morphMany(Pivot::class, 'facturable'); }
    }
    // src/ConUse.php  — identical, but with a redundant same-namespace import
    <?php
    namespace App\Entities;
    
    use App\Entities\Pivot;
    
    class ConUse
    {
        public function rows() { return $this->morphMany(Pivot::class, 'facturable'); }
    }
    $ graphify explain "Pivot"
      Degree: 2
      <-- ConUse.php [imports]  src/ConUse.php:L4
      <-- Pivot.php  [contains] src/Pivot.php:L3

    SinUse.php never appears, though both files reference Pivot identically. The edge tracks the use statement, not the actual reference.

    Why this is load-bearing for PHP. Two very common shapes have no use to key off:

    • the target lives in the same namespace, so use is redundant (linters flag it as unnecessary);
    • the name is written fully qualified inline: \App\Entities\Pivot::class.

    Measured on the real project with 0.9.41 — of 240 Eloquent relationships declared in app/ (hasMany, belongsTo, morphMany, morphTo, …), 158 (65%) still produce no edge. Only the 82 that happen to carry a use are linked:

    mismo namespace (no use)        140  across 54 files
    fully-qualified inline           18  across  6 files
    

    Since Model::class inside a method body is how Eloquent declares every relationship, the ORM layer — the densest part of the real dependency graph — stays largely invisible. affected on an Entity still returns an empty result that reads as "nothing depends on this" rather than "unknown".

    Adding redundant use statements does make the edges appear, but that means editing production code to satisfy the extractor, and any IDE "optimize imports" pass silently reverts it — so it trades a consistent gap for an intermittent one.

    Expected: X::class inside a method body resolves against the current namespace (and any use aliases) and produces a references edge from the enclosing method, the same way a constructor type hint already does.

    Glad to test a patch on the real project — heavy Eloquent plus prettus/l5-repository, so it exercises both shapes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions