Hideable, Taggable, Lockable, FuturePublishing and the rest are traits, not interfaces. Testing for them with instanceof compiles, runs, throws nothing — and is false for every object on the site.
Wrong
if ( $item instanceof \IPS\Content\Taggable ) // always FALSE
{
$item->setTags( $tags );
}
The feature simply never runs. Nothing in a log, nothing in a test that only asserts "no exception was thrown".
Right
if ( \IPS\IPS::classUsesTrait( $item, 'IPS\Content\Taggable' ) )
{
$item->setTags( $tags );
}
classUsesTrait() lives in init.php and accepts either an object or a class name. Core uses it everywhere for exactly this reason:
if ( IPS::classUsesTrait( $topic, 'IPS\Content\Hideable' ) and $topic->hidden() )
How to catch it
php -l cannot see this, and neither can a test that only checks a method returned without error. Assert the positive case explicitly: that a known-taggable class is detected as taggable.
Recommended Comments