Issues (73)

src/Metadata/Parser/IPTCParser.php (2 issues)

1
<?php
2
3
namespace Jackal\ImageMerge\Metadata\Parser;
4
5
use DateTime;
6
use Exception;
7
use Jackal\ImageMerge\Model\File\FileObjectInterface;
8
9
/**
10
 * Class IPTCParser
11
 * @package Jackal\ImageMerge\Metadata\Parser
12
 */
13
class IPTCParser extends AbstractParser
14
{
15
    const TITLE = '2#005';
16
    const URGENCY = '2#010';
17
    const CATEGORY = '2#015';
18
    const SUB_CATEGORY = '2#020';
19
    const SPECIAL_INSTRUCTION = '2#040';
20
    const CREATION_DATE = '2#055';
21
    const CREATION_TIME = '2#060';
22
    const DIGITAL_CREATION_DATE = '2#062';
23
    const DIGITAL_CREATION_TIME = '2#063';
24
    const BY_LINE = '2#080';
25
    const BY_LINE_TITLE = '2#085';
26
    const CITY = '2#090';
27
    const LOCATION = '2#092';
28
    const STATE = '2#095';
29
    const COUNTRY_CODE = '2#100';
30
    const COUNTRY_NAME = '2#101';
31
    const OTR = '2#103';
32
    const HEADLINE = '2#105';
33
    const CREDIT = '2#110';
34
    const SOURCE = '2#115';
35
    const COPYRIGHT = '2#116';
36
    const CONTACT = '2#118';
37
    const CAPTION = '2#120';
38
    const CAPTION_WRITER = '2#122';
39
    const CHARSET = '1#090';
40
    const KEYWORDS = '2#025';
41
42
    /**
43
     * IPTCParser constructor.
44
     * @param FileObjectInterface $file
45
     */
46
    public function __construct(FileObjectInterface $file)
47
    {
48
        @iptcembed('', $file->getPathname(), 0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for iptcembed(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

48
        /** @scrutinizer ignore-unhandled */ @iptcembed('', $file->getPathname(), 0);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
49
        $info = null;
50
        getimagesize($file->getPathname(), $info);
51
52
        if (isset($info['APP13'])) {
53
            $this->data = iptcparse($info['APP13']);
0 ignored issues
show
Documentation Bug introduced by
It seems like iptcparse($info['APP13']) can also be of type false. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
        }
55
    }
56
57
    /**
58
     * @return array|null|string
59
     */
60
    public function getCategory()
61
    {
62
        return $this->getValue(self::CATEGORY);
63
    }
64
65
    /**
66
     * @return DateTime|null
67
     * @throws Exception
68
     */
69
    public function getCreationDateTime()
70
    {
71
        if ($this->getSingleValue(self::CREATION_DATE)) {
72
            $dt = trim($this->getSingleValue(self::CREATION_DATE) . ' ' . $this->getSingleValue(self::CREATION_TIME));
73
74
            return new DateTime($dt);
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * @return array|null|string
82
     */
83
    public function getKeywords()
84
    {
85
        return $this->getValue(self::KEYWORDS);
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function isUTF8()
92
    {
93
        return $this->getSingleValue(self::CHARSET) == "\x1B%G";
94
    }
95
96
    /**
97
     * @return null|string
98
     */
99
    public function getCreator()
100
    {
101
        return $this->getSingleValue(self::CAPTION_WRITER);
102
    }
103
104
    /**
105
     * @return null|string
106
     */
107
    public function getDescription()
108
    {
109
        return $this->getSingleValue(self::CAPTION);
110
    }
111
112
    public function getCopyrights()
113
    {
114
        return $this->getSingleValue(self::COPYRIGHT);
115
    }
116
117
    /**
118
     * @return array
119
     * @throws Exception
120
     */
121
    public function toArray()
122
    {
123
        return [
124
            'category' => $this->getCategory(),
125
            'created_at' => $this->getCreationDateTime(),
126
            'keywords' => $this->getKeywords(),
127
            'utf8' => $this->isUTF8(),
128
            'created_by' => $this->getCreator(),
129
            'description' => $this->getDescription(),
130
            'copyrights' => $this->getCopyrights(),
131
        ];
132
    }
133
}
134