Issues

ZF-8598: Zend_Validate_File_IsImage class constant inheritance issue

Description

The use of class constants in Zend_Validate_File_IsImage is incorrect. See the following code example:


<?php 

class Dad
{
    const HAIR_COLOR = "Brown";
    const EYE_COLOR = "Blue";
    public function getHairColor()
    {
        return self::HAIR_COLOR;
    }
    public function getShirtColor()
    {
        return self::SHIRT_COLOR;
    }
}

class Son extends Dad
{
    const HAIR_COLOR = "Red";
    public function getChildHairColor()
    {
        return self::HAIR_COLOR;
    }
    public function getEyeColor()
    {
        return self::EYE_COLOR;
    }
}

$son = new Son();
echo $son->getHairColor(); // Echoes "Brown"
echo $son->getChildHairColor(); // Echoes "Red"
echo $son->getEyeColor(); // Echoes "Blue"

Because Zend_Validate_File_IsImage redefines constants that are then used in the parent Zend_Validate_File_MimeType class, certain errors have blank messages.

For example, on line 317 of Zend/Validate/File/MimeType:


if (empty($this->_type)) {
    return $this->_throw($file, self::NOT_DETECTED);
}

{{self::NOT_DETECTED}} refers to 'fileMimeTypeNotDetected' which 'fileIsImageNotDetected' in Zend_Validate_File_IsImage. And because the $_messageTemplates property has been overwritten in Zend_Validate_File_IsImage, the method looks for $this->_messageTemplates['fileMimeTypeNotDetected'] which no longer exists.

The solution would be to remove these constants from the top of Zend_Validate_File_IsImage, as the are already defined in the parent:


const FALSE_TYPE   = 'fileIsImageFalseType';
const NOT_DETECTED = 'fileIsImageNotDetected';
const NOT_READABLE = 'fileIsImageNotReadable';

Comments

Attached is a patch to fix this issue. It just removes those constant definitions.

Unable to reproduce

Wether on 5.2 nor on 5.3 the unittests show that a false message content is thrown.

The problem is late static binding in PHP 5.2 as explained in this related issue