Skip to content

[BUG]: TagFactory::newInstance() returns the factory value but declares object #17785

Todo

Description

@niden

Describe the bug

Phalcon\Html\TagFactory::newInstance() declares the return type object (phalcon/Html/TagFactory.zep:198), but it returns the value of the factory with no check:

public function newInstance(string name) -> object
{
    var factory;

    if !isset this->factories[name] {
        throw new ServiceNotRegistered(name);
    }

    if !isset this->instances[name] {
        let factory = this->factories[name];
        let this->instances[name] = call_user_func(factory);
    }

    return this->instances[name];
}

A factory that is registered with the constructor or with set() can return a value that is not an object, for example static fn () => 5.

  • A release PHP build does not check the return type of internal functions. newInstance() returns the value.
  • A PHP debug build (--enable-debug) checks the return type and stops with a fatal error.

The PHP implementation (phalcon/phalcon, src/Html/TagFactory.php) declares mixed and returns the value as it is, "the same as cphalcon" (phalcon/phalcon#818). The test NewInstanceTest::testFilterTagFactoryNewInstanceNotObject in both projects expects this behavior.

To Reproduce

PHP compiled with --enable-debug:

<?php

$factory = new Phalcon\Html\TagFactory(
    new Phalcon\Html\Escaper(),
    ['five' => static fn () => 5]
);

var_dump($factory->newInstance('five'));

Output:

Fatal error: Phalcon\Html\TagFactory::newInstance(): Return value must be of type object, int returned

The test suite stops at the same error in tests/unit/Html/TagFactory/NewInstanceTest.php:143 (testFilterTagFactoryNewInstanceNotObject). With a release PHP build, the same script prints int(5).

Expected behavior

The declared return type agrees with the returned values, the same as the PHP implementation.

Possible fix

Declare the return type mixed. The @phpstan-return ($name is 'doctype' ? Doctype : object) docblock does not change:

public function newInstance(string name) -> mixed

The returned values do not change. TagFactory has no parent class and no interface. A class that extends TagFactory and overrides newInstance() with the return type object stays compatible.

Details

  • Phalcon version: 5.23.0 (5.0.x)
  • PHP Version: 8.6.0 with --enable-debug
  • Operating System: Debian (docker)
  • Installation type: Compiling from source

Activity

  1. added
    bug
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
  3. added
    5.0
    on Nov 24, 2026
  4. added theissue type on Nov 24, 2026
  5. added and removed
    5.0
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

7.0bugstatus: medium

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions