|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the kaloa/image package. |
|
5
|
|
|
* |
|
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
|
7
|
|
|
* that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Kaloa\Image; |
|
11
|
|
|
|
|
12
|
|
|
use Kaloa\Image\Filter\FilterInterface; |
|
13
|
|
|
use ReflectionClass; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ImageFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var array Array of FilterInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $filters = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @return Image |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public function createNewImage() |
|
31
|
|
|
{ |
|
32
|
6 |
|
return new Image($this); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
*/ |
|
38
|
1 |
|
private function rebuildFilters() |
|
39
|
|
|
{ |
|
40
|
1 |
|
$files = glob(__DIR__ . '/Filter/*Filter.php'); |
|
41
|
|
|
|
|
42
|
1 |
|
$this->filters = array(); |
|
43
|
|
|
|
|
44
|
1 |
|
foreach ($files as $file) { |
|
45
|
1 |
|
$basename = basename($file); |
|
46
|
|
|
|
|
47
|
1 |
|
if ($basename === 'AbstractFilter.php') { |
|
48
|
1 |
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$className = 'Kaloa\\Image\\Filter\\' . substr($basename, 0, -4); |
|
52
|
1 |
|
$methodName = lcfirst(substr($basename, 0, -10)); |
|
53
|
|
|
|
|
54
|
1 |
|
$this->filters[$methodName] = $className; |
|
55
|
1 |
|
} |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $methodName |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function _hasFilter($methodName) |
|
64
|
|
|
{ |
|
65
|
|
|
// Lazy loading |
|
66
|
1 |
|
if (null === $this->filters) { |
|
67
|
1 |
|
$this->rebuildFilters(); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return (isset($this->filters[$methodName])); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $methodName |
|
76
|
|
|
* @param array $arguments |
|
77
|
|
|
* @return FilterInterface |
|
78
|
|
|
* @throws ImageException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function _getFilterInstance($methodName, $arguments) |
|
81
|
|
|
{ |
|
82
|
|
|
// Lazy loading |
|
83
|
1 |
|
if (null === $this->filters) { |
|
84
|
|
|
$this->rebuildFilters(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
$instance = null; |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
1 |
|
if (!$this->_hasFilter($methodName)) { |
|
90
|
|
|
throw new ImageException('Unknown filter: ' . $methodName); |
|
91
|
|
|
} else { |
|
92
|
1 |
|
$reflection = new ReflectionClass($this->filters[$methodName]); |
|
93
|
1 |
|
$instance = $reflection->newInstanceArgs($arguments); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return $instance; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.