Skip to content

Commit 4445fd3

Browse files
author
Roman Syroeshko
committed
Replaced array_key_exists with isset for better performance.
1 parent 9d73173 commit 4445fd3

File tree

22 files changed

+64
-66
lines changed

22 files changed

+64
-66
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function __call($function, $args)
9292

9393
// Run valid `add` command
9494
$function = strtolower($function);
95-
if (array_key_exists($function, $functions)) {
95+
if (isset($functions[$function])) {
9696
$element = $functions[$function];
9797

9898
// Special case for TextBreak
@@ -183,23 +183,22 @@ public function countElements()
183183
*/
184184
private function checkValidity($method)
185185
{
186-
// Valid containers for each element
187-
$allContainers = array(
188-
'Section', 'Header', 'Footer', 'Footnote', 'Endnote',
189-
'Cell', 'TextRun', 'TextBox', 'ListItemRun',
186+
$generalContainers = array(
187+
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
190188
);
189+
191190
$validContainers = array(
192-
'Text' => $allContainers,
193-
'Bookmark' => $allContainers,
194-
'Link' => $allContainers,
195-
'TextBreak' => $allContainers,
196-
'Image' => $allContainers,
197-
'Object' => $allContainers,
198-
'Field' => $allContainers,
199-
'Line' => $allContainers,
200-
'Shape' => $allContainers,
201-
'FormField' => $allContainers,
202-
'SDT' => $allContainers,
191+
'Text' => $generalContainers,
192+
'Bookmark' => $generalContainers,
193+
'Link' => $generalContainers,
194+
'TextBreak' => $generalContainers,
195+
'Image' => $generalContainers,
196+
'Object' => $generalContainers,
197+
'Field' => $generalContainers,
198+
'Line' => $generalContainers,
199+
'Shape' => $generalContainers,
200+
'FormField' => $generalContainers,
201+
'SDT' => $generalContainers,
203202
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
204203
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
205204
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
@@ -214,6 +213,7 @@ private function checkValidity($method)
214213
'PageBreak' => array('Section'),
215214
'Chart' => array('Section'),
216215
);
216+
217217
// Special condition, e.g. preservetext can only exists in cell when
218218
// the cell is located in header or footer
219219
$validSubcontainers = array(
@@ -223,19 +223,20 @@ private function checkValidity($method)
223223
);
224224

225225
// Check if a method is valid for current container
226-
if (array_key_exists($method, $validContainers)) {
226+
if (isset($validContainers[$method])) {
227227
if (!in_array($this->container, $validContainers[$method])) {
228-
throw new \BadMethodCallException("Cannot add $method in $this->container.");
228+
throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
229229
}
230230
}
231+
231232
// Check if a method is valid for current container, located in other container
232-
if (array_key_exists($method, $validSubcontainers)) {
233+
if (isset($validSubcontainers[$method])) {
233234
$rules = $validSubcontainers[$method];
234235
$containers = $rules[0];
235236
$allowedDocParts = $rules[1];
236237
foreach ($containers as $container) {
237238
if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
238-
throw new \BadMethodCallException("Cannot add $method in $this->container.");
239+
throw new \BadMethodCallException("Cannot add {$method} in {$this->container}.");
239240
}
240241
}
241242
}

src/PhpWord/Element/Field.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __construct($type = null, $properties = array(), $options = arra
100100
public function setType($type = null)
101101
{
102102
if (isset($type)) {
103-
if (array_key_exists($type, $this->fieldsArray)) {
103+
if (isset($this->fieldsArray[$type])) {
104104
$this->type = $type;
105105
} else {
106106
throw new \InvalidArgumentException("Invalid type");
@@ -130,7 +130,7 @@ public function setProperties($properties = array())
130130
{
131131
if (is_array($properties)) {
132132
foreach (array_keys($properties) as $propkey) {
133-
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
133+
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
134134
throw new \InvalidArgumentException("Invalid property");
135135
}
136136
}
@@ -160,7 +160,7 @@ public function setOptions($options = array())
160160
{
161161
if (is_array($options)) {
162162
foreach (array_keys($options) as $optionkey) {
163-
if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) {
163+
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) {
164164
throw new \InvalidArgumentException("Invalid option");
165165
}
166166
}

src/PhpWord/Element/Title.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ class Title extends AbstractElement
6161
*/
6262
public function __construct($text, $depth = 1)
6363
{
64-
6564
$this->text = String::toUTF8($text);
6665
$this->depth = $depth;
67-
if (array_key_exists('Heading_' . $this->depth, Style::getStyles())) {
68-
$this->style = 'Heading' . $this->depth;
66+
if (array_key_exists("Heading_{$this->depth}", Style::getStyles())) {
67+
$this->style = "Heading{$this->depth}";
6968
}
7069

7170
return $this;

src/PhpWord/Media.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public static function addElement($container, $mediaType, $source, Image $image
4848
{
4949
// Assign unique media Id and initiate media container if none exists
5050
$mediaId = md5($container . $source);
51-
if (!array_key_exists($container, self::$elements)) {
51+
if (!isset(self::$elements[$container])) {
5252
self::$elements[$container] = array();
5353
}
5454

5555
// Add media if not exists or point to existing media
56-
if (!array_key_exists($mediaId, self::$elements[$container])) {
56+
if (!isset(self::$elements[$container][$mediaId])) {
5757
$mediaCount = self::countElements($container);
5858
$mediaTypeCount = self::countElements($container, $mediaType);
5959
$mediaTypeCount++;
@@ -120,7 +120,7 @@ public static function countElements($container, $mediaType = null)
120120
{
121121
$mediaCount = 0;
122122

123-
if (array_key_exists($container, self::$elements)) {
123+
if (isset(self::$elements[$container])) {
124124
foreach (self::$elements[$container] as $mediaData) {
125125
if (!is_null($mediaType)) {
126126
if ($mediaType == $mediaData['type']) {
@@ -156,7 +156,7 @@ public static function getElements($container, $type = null)
156156
}
157157
return $elements;
158158
} else {
159-
if (!array_key_exists($container, self::$elements)) {
159+
if (!isset(self::$elements[$container])) {
160160
return $elements;
161161
}
162162
return self::getElementsByType($container, $type);

src/PhpWord/PhpWord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function __call($function, $args)
139139
/** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
140140
$collectionObject = $this->collections[$key];
141141

142-
return $collectionObject->addItem(array_key_exists(0, $args) ? $args[0] : null);
142+
return $collectionObject->addItem(isset($args[0]) ? $args[0] : null);
143143
}
144144

145145
// Run add style method

src/PhpWord/Reader/RTF/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function read(PhpWord $phpWord)
155155
$char = $this->rtf[$this->offset];
156156
$ascii = ord($char);
157157

158-
if (array_key_exists($ascii, $markers)) { // Marker found: {, }, \, LF, or CR
158+
if (isset($markers[$ascii])) { // Marker found: {, }, \, LF, or CR
159159
$markerFunction = $markers[$ascii];
160160
$this->$markerFunction();
161161
} else {
@@ -351,7 +351,7 @@ private function parseControl($control, $parameter)
351351
'fldinst' => array(self::SKIP, 'link', null),
352352
);
353353

354-
if (array_key_exists($control, $controls)) {
354+
if (isset($controls[$control])) {
355355
list($function) = $controls[$control];
356356
if (method_exists($this, $function)) {
357357
$directives = $controls[$control];

src/PhpWord/Reader/Word2007.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function load($docFile)
6363
$stepItems = $step['stepItems'];
6464
foreach ($relationships[$stepPart] as $relItem) {
6565
$relType = $relItem['type'];
66-
if (array_key_exists($relType, $stepItems)) {
66+
if (isset($stepItems[$relType])) {
6767
$partName = $stepItems[$relType];
6868
$xmlFile = $relItem['target'];
6969
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
107107
$headingMatches = array();
108108
if ($xmlReader->elementExists('w:pPr', $domNode)) {
109109
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
110-
if (is_array($paragraphStyle) && array_key_exists('styleName', $paragraphStyle)) {
110+
if (is_array($paragraphStyle) && isset($paragraphStyle['styleName'])) {
111111
preg_match('/Heading(\d)/', $paragraphStyle['styleName'], $headingMatches);
112112
}
113113
}
@@ -505,10 +505,9 @@ private function readStyleDef($method, $attributeValue, $expected)
505505
private function getMediaTarget($docPart, $rId)
506506
{
507507
$target = null;
508-
if (array_key_exists($docPart, $this->rels)) {
509-
if (array_key_exists($rId, $this->rels[$docPart])) {
510-
$target = $this->rels[$docPart][$rId]['target'];
511-
}
508+
509+
if (isset($this->rels[$docPart]) && isset($this->rels[$docPart][$rId])) {
510+
$target = $this->rels[$docPart][$rId]['target'];
512511
}
513512

514513
return $target;

src/PhpWord/Reader/Word2007/DocPropsCore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public function read(PhpWord $phpWord)
6767
$nodes = $xmlReader->getElements('*');
6868
if ($nodes->length > 0) {
6969
foreach ($nodes as $node) {
70-
if (!array_key_exists($node->nodeName, $this->mapping)) {
70+
if (!isset($this->mapping[$node->nodeName])) {
7171
continue;
7272
}
7373
$method = $this->mapping[$node->nodeName];
7474
$value = $node->nodeValue == '' ? null : $node->nodeValue;
75-
if (array_key_exists($node->nodeName, $this->callbacks)) {
75+
if (isset($this->callbacks[$node->nodeName])) {
7676
$value = $this->callbacks[$node->nodeName]($value);
7777
}
7878
if (method_exists($docProps, $method)) {

src/PhpWord/Reader/Word2007/Document.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function read(PhpWord $phpWord)
5353
if ($nodes->length > 0) {
5454
$section = $this->phpWord->addSection();
5555
foreach ($nodes as $node) {
56-
if (array_key_exists($node->nodeName, $readMethods)) {
56+
if (isset($readMethods[$node->nodeName])) {
5757
$readMethod = $readMethods[$node->nodeName];
5858
$this->$readMethod($xmlReader, $node, $section);
5959
}
@@ -72,9 +72,9 @@ private function readHeaderFooter($settings, Section &$section)
7272
{
7373
$readMethods = array('w:p' => 'readParagraph', 'w:tbl' => 'readTable');
7474

75-
if (is_array($settings) && array_key_exists('hf', $settings)) {
75+
if (is_array($settings) && isset($settings['hf'])) {
7676
foreach ($settings['hf'] as $rId => $hfSetting) {
77-
if (array_key_exists($rId, $this->rels['document'])) {
77+
if (isset($this->rels['document'][$rId])) {
7878
list($hfType, $xmlFile, $docPart) = array_values($this->rels['document'][$rId]);
7979
$addMethod = "add{$hfType}";
8080
$hfObject = $section->$addMethod($hfSetting['type']);
@@ -85,7 +85,7 @@ private function readHeaderFooter($settings, Section &$section)
8585
$nodes = $xmlReader->getElements('*');
8686
if ($nodes->length > 0) {
8787
foreach ($nodes as $node) {
88-
if (array_key_exists($node->nodeName, $readMethods)) {
88+
if (isset($readMethods[$node->nodeName])) {
8989
$readMethod = $readMethods[$node->nodeName];
9090
$this->$readMethod($xmlReader, $node, $hfObject, $docPart);
9191
}

0 commit comments

Comments
 (0)