Repository navigation
Description
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) -> mixedThe 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity