| Total Complexity | 40 |
| Total Lines | 231 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 2 | Features | 2 |
Complex classes like Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Page extends PDFObject |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var Font[] |
||
| 47 | */ |
||
| 48 | protected $fonts = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var PDFObject[] |
||
| 52 | */ |
||
| 53 | protected $xobjects = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return Font[] |
||
| 57 | */ |
||
| 58 | public function getFonts() |
||
| 59 | { |
||
| 60 | if (!is_null($this->fonts)) { |
||
|
|
|||
| 61 | return $this->fonts; |
||
| 62 | } |
||
| 63 | |||
| 64 | $resources = $this->get('Resources'); |
||
| 65 | |||
| 66 | if (method_exists($resources, 'has') && $resources->has('Font')) { |
||
| 67 | |||
| 68 | if ($resources->get('Font') instanceof Header) { |
||
| 69 | $fonts = $resources->get('Font')->getElements(); |
||
| 70 | } else { |
||
| 71 | $fonts = $resources->get('Font')->getHeader()->getElements(); |
||
| 72 | } |
||
| 73 | |||
| 74 | $table = array(); |
||
| 75 | |||
| 76 | foreach ($fonts as $id => $font) { |
||
| 77 | if ($font instanceof Font) { |
||
| 78 | $table[$id] = $font; |
||
| 79 | |||
| 80 | // Store too on cleaned id value (only numeric) |
||
| 81 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 82 | if ($id != '') { |
||
| 83 | $table[$id] = $font; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return ($this->fonts = $table); |
||
| 89 | } else { |
||
| 90 | return array(); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $id |
||
| 96 | * |
||
| 97 | * @return Font |
||
| 98 | */ |
||
| 99 | public function getFont($id) |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Support for XObject |
||
| 118 | * |
||
| 119 | * @return PDFObject[] |
||
| 120 | */ |
||
| 121 | public function getXObjects() |
||
| 122 | { |
||
| 123 | if (!is_null($this->xobjects)) { |
||
| 124 | return $this->xobjects; |
||
| 125 | } |
||
| 126 | |||
| 127 | $resources = $this->get('Resources'); |
||
| 128 | |||
| 129 | if (method_exists($resources, 'has') && $resources->has('XObject')) { |
||
| 130 | |||
| 131 | if ($resources->get('XObject') instanceof Header) { |
||
| 132 | $xobjects = $resources->get('XObject')->getElements(); |
||
| 133 | } else { |
||
| 134 | $xobjects = $resources->get('XObject')->getHeader()->getElements(); |
||
| 135 | } |
||
| 136 | |||
| 137 | $table = array(); |
||
| 138 | |||
| 139 | foreach ($xobjects as $id => $xobject) { |
||
| 140 | $table[$id] = $xobject; |
||
| 141 | |||
| 142 | // Store too on cleaned id value (only numeric) |
||
| 143 | $id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 144 | if ($id != '') { |
||
| 145 | $table[$id] = $xobject; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return ($this->xobjects = $table); |
||
| 150 | } else { |
||
| 151 | return array(); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $id |
||
| 157 | * |
||
| 158 | * @return PDFObject |
||
| 159 | */ |
||
| 160 | public function getXObject($id) |
||
| 161 | { |
||
| 162 | $xobjects = $this->getXObjects(); |
||
| 163 | |||
| 164 | if (isset($xobjects[$id])) { |
||
| 165 | return $xobjects[$id]; |
||
| 166 | } else { |
||
| 167 | return null; |
||
| 168 | /*$id = preg_replace('/[^0-9\.\-_]/', '', $id); |
||
| 169 | |||
| 170 | if (isset($xobjects[$id])) { |
||
| 171 | return $xobjects[$id]; |
||
| 172 | } else { |
||
| 173 | return null; |
||
| 174 | }*/ |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param Page |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getText(Page $page = null) |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Page |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function getTextArray(Page $page = null) |
||
| 274 | } |
||
| 275 | } |
||
| 276 |