Skip to content

InaccessibleMethod on a trait method made private by a use adaptation when the parent has __call() #14020

Closed

Description

@orklah

When a class makes a public trait method private with a use adaptation (product as private) and its parent declares __call(), a call from outside the class should go through __call(), because PHP calls __call() for an inaccessible method. Psalm reports InaccessibleMethod instead.

https://psalm.dev/r/f33e45893a

<?php
class ParentClass {
    public function __call(string $name, array $args): int {
        return 1;
    }
}

trait RealTrait {
    public function product(): string {
        return "Screen";
    }
}

final class Child extends ParentClass {
    use RealTrait {
        product as private;
    }
}

(new Child)->product(); // InaccessibleMethod - Cannot access private method Child::product

A private method declared directly in Child goes through the magic-method path, as expected (https://psalm.dev/r/dd8cdcf5cd: UndefinedMagicMethod, no InaccessibleMethod).

With /** @method int product() */ on ParentClass (https://psalm.dev/r/8202a5e697), the trait case still reports InaccessibleMethod, and the result is inferred as string, the trait method's return type, instead of int. With a private method declared in Child, there is no InaccessibleMethod and the result is int.

Expected: the privately aliased trait method behaves like a private method declared in the class. There's no InaccessibleMethod, the call resolves through __call(), and $a is int with the @method annotation.

Activity

  1. psalm-github-bot commented on Nov 16, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/f33e45893a
    <?php
    class ParentClass {
        public function __call(string $name, array $args): int {
            return 1;
        }
    }
    
    trait RealTrait {
        public function product(): string {
            return "Screen";
        }
    }
    
    final class Child extends ParentClass {
        use RealTrait {
            product as private;
        }
    }
    
    (new Child)->product();
    Psalm output (using commit f349d3a):
    
    ERROR: InaccessibleMethod - 20:14 - Cannot access private method Child::product
    
    https://psalm.dev/r/dd8cdcf5cd
    <?php
    class ParentClass {
        public function __call(string $name, array $args): int {
            return 1;
        }
    }
    
    final class Child extends ParentClass {
        private function product(): string {
            return "Screen";
        }
    }
    
    (new Child)->product();
    Psalm output (using commit f349d3a):
    
    ERROR: UndefinedMagicMethod - 14:14 - Magic method Child::product does not exist
    
    https://psalm.dev/r/8202a5e697
    <?php
    /** @method int product() */
    class ParentClass {
        public function __call(string $name, array $args): int {
            return 1;
        }
    }
    
    trait RealTrait {
        public function product(): string {
            return "Screen";
        }
    }
    
    final class Child extends ParentClass {
        use RealTrait {
            product as private;
        }
    }
    
    $a = (new Child)->product();
    /** @psalm-trace $a */;
    Psalm output (using commit f349d3a):
    
    ERROR: InaccessibleMethod - 21:19 - Cannot access private method Child::product
    
    INFO: Trace - 22:23 - $a: int
    
    INFO: UnusedVariable - 21:1 - $a is never referenced or the value is not used
    
    ERROR: InvalidReturnType - 10:32 - The declared return type 'int' for RealTrait::product is incorrect, got ''Screen''
    
  2. orklah commented on Nov 16, 2026

    @orklah
    CollaboratorAuthor

    The same cause shows up in the other direction: a private trait method made protected by an adaptation in a parent class is treated as not visible from the child, so a __call() in the grandparent takes the call over.

    https://psalm.dev/r/62b0c8bce2

    <?php
    trait Impl {
        private function impl(): string { return ""; }
    }
    
    class P {
        public function __call(string $n, array $a): mixed { return null; }
    }
    
    class R2 extends P {
        use Impl { impl as protected product; }
    }
    
    final class R3 extends R2 {
        public function t(): string {
            $a = $this->product(); // UndefinedMagicMethod - Magic method R3::product does not exist
            /** @psalm-trace $a */; // $a: mixed
            return $a;
        }
    }

    PHP runs Impl::impl() here, so $a should be string with no issue. With /** @method int product() */ on P (https://psalm.dev/r/1a31cc0972), $a is int. Without __call() on P (https://psalm.dev/r/01a6767b83), $a is string, as expected.

    MethodAnalyzer::isMethodVisible() reads the visibility of the declaring method (private in the trait, public in the snippet above) and ignores the trait_visibility_map of the class where the method appears, which MethodVisibilityAnalyzer does take into account.

  3. psalm-github-bot commented on Nov 16, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/62b0c8bce2
    <?php
    trait Impl {
        private function impl(): string { return ""; }
    }
    
    class P {
        public function __call(string $n, array $a): mixed { return null; }
    }
    
    class R2 extends P {
        use Impl { impl as protected product; }
    }
    
    final class R3 extends R2 {
        public function t(): string {
            $a = $this->product();
            /** @psalm-trace $a */;
            return $a;
        }
    }
    Psalm output (using commit 30edada):
    
    ERROR: UndefinedMagicMethod - 16:21 - Magic method R3::product does not exist
    
    INFO: MixedAssignment - 16:9 - Unable to determine the type that $a is being assigned to
    
    INFO: Trace - 17:31 - $a: mixed
    
    INFO: MixedReturnStatement - 18:16 - Could not infer a return type
    
    https://psalm.dev/r/1a31cc0972
    <?php
    trait Impl {
        private function impl(): string { return ""; }
    }
    
    /** @method int product() */
    class P {
        public function __call(string $n, array $a): mixed { return null; }
    }
    
    class R2 extends P {
        use Impl { impl as protected product; }
    }
    
    final class R3 extends R2 {
        public function t(): string {
            $a = $this->product();
            /** @psalm-trace $a */;
            return $a;
        }
    }
    Psalm output (using commit 30edada):
    
    INFO: Trace - 18:31 - $a: int
    
    ERROR: InvalidReturnStatement - 19:16 - The inferred type 'int' does not match the declared return type 'string' for R3::t
    
    ERROR: InvalidReturnType - 16:26 - The declared return type 'string' for R3::t is incorrect, got 'int'
    
    https://psalm.dev/r/01a6767b83
    <?php
    trait Impl {
        private function impl(): string { return ""; }
    }
    
    class P {
    }
    
    class R2 extends P {
        use Impl { impl as protected product; }
    }
    
    final class R3 extends R2 {
        public function t(): string {
            $a = $this->product();
            /** @psalm-trace $a */;
            return $a;
        }
    }
    Psalm output (using commit 30edada):
    
    INFO: Trace - 16:31 - $a: string
    
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