Skip to content

Commit 1983f77

Browse files
committed
Some code optimizations (thanks to @codeclimate)
1 parent a84e729 commit 1983f77

File tree

17 files changed

+263
-344
lines changed

17 files changed

+263
-344
lines changed

src/PhpPresentation/Reader/PowerPoint97.php

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,10 @@ private function loadPicturesStream()
465465
$stream = $this->streamPictures;
466466

467467
$pos = 0;
468-
$readSuccess = true;
469468
do {
470469
$arrayRH = $this->loadRecordHeader($stream, $pos);
471470
$pos += 8;
471+
$readSuccess = false;
472472
if ($arrayRH['recVer'] == 0x00 && ($arrayRH['recType'] == 0xF007 || ($arrayRH['recType'] >= 0xF018 && $arrayRH['recType'] <= 0xF117))) {
473473
//@link : http://msdn.microsoft.com/en-us/library/dd950560(v=office.12).aspx
474474
if ($arrayRH['recType'] == 0xF007) {
@@ -482,8 +482,7 @@ private function loadPicturesStream()
482482
$this->arrayPictures[] = $arrayRecord['picture'];
483483
}
484484
}
485-
} else {
486-
$readSuccess = false;
485+
$readSuccess = true;
487486
}
488487
} while ($readSuccess === true);
489488
}
@@ -678,11 +677,11 @@ public static function getInt4d($data, $pos)
678677
// http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
679678
// Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
680679
$or24 = ord($data[$pos + 3]);
680+
681+
$ord24 = ($or24 & 127) << 24;
681682
if ($or24 >= 128) {
682683
// negative number
683684
$ord24 = -abs((256 - $or24) << 24);
684-
} else {
685-
$ord24 = ($or24 & 127) << 24;
686685
}
687686
return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | $ord24;
688687
}
@@ -826,37 +825,36 @@ private function readRecordDocumentContainer($stream, $pos)
826825
$fontCollection['recLen'] -= 8;
827826
if ($fontEntityAtom['recVer'] != 0x0 || $fontEntityAtom['recInstance'] > 128 || $fontEntityAtom['recType'] != self::RT_FONTENTITYATOM) {
828827
throw new \Exception('File PowerPoint 97 in error (Location : RTDocument > RT_Environment > RT_FontCollection > RT_FontEntityAtom).');
829-
} else {
830-
$string = '';
831-
for ($inc = 0; $inc < 32; $inc++) {
832-
$char = self::getInt2d($stream, $pos);
833-
$pos += 2;
834-
$fontCollection['recLen'] -= 2;
835-
$string .= chr($char);
836-
}
837-
$this->arrayFonts[] = $string;
838-
839-
// lfCharSet (1 byte)
840-
$pos += 1;
841-
$fontCollection['recLen'] -= 1;
842-
843-
// fEmbedSubsetted (1 bit)
844-
// unused (7 bits)
845-
$pos += 1;
846-
$fontCollection['recLen'] -= 1;
847-
848-
// rasterFontType (1 bit)
849-
// deviceFontType (1 bit)
850-
// truetypeFontType (1 bit)
851-
// fNoFontSubstitution (1 bit)
852-
// reserved (4 bits)
853-
$pos += 1;
854-
$fontCollection['recLen'] -= 1;
855-
856-
// lfPitchAndFamily (1 byte)
857-
$pos += 1;
858-
$fontCollection['recLen'] -= 1;
859828
}
829+
$string = '';
830+
for ($inc = 0; $inc < 32; $inc++) {
831+
$char = self::getInt2d($stream, $pos);
832+
$pos += 2;
833+
$fontCollection['recLen'] -= 2;
834+
$string .= chr($char);
835+
}
836+
$this->arrayFonts[] = $string;
837+
838+
// lfCharSet (1 byte)
839+
$pos += 1;
840+
$fontCollection['recLen'] -= 1;
841+
842+
// fEmbedSubsetted (1 bit)
843+
// unused (7 bits)
844+
$pos += 1;
845+
$fontCollection['recLen'] -= 1;
846+
847+
// rasterFontType (1 bit)
848+
// deviceFontType (1 bit)
849+
// truetypeFontType (1 bit)
850+
// fNoFontSubstitution (1 bit)
851+
// reserved (4 bits)
852+
$pos += 1;
853+
$fontCollection['recLen'] -= 1;
854+
855+
// lfPitchAndFamily (1 byte)
856+
$pos += 1;
857+
$fontCollection['recLen'] -= 1;
860858

861859
$fontEmbedData1 = $this->loadRecordHeader($stream, $pos);
862860
if ($fontEmbedData1['recVer'] == 0x0 && $fontEmbedData1['recInstance'] >= 0x000 && $fontEmbedData1['recInstance'] <= 0x003 && $fontEmbedData1['recType'] == self::RT_FONTEMBEDDATABLOB) {

src/PhpPresentation/Shape/Drawing.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,15 @@ public function getPath()
9696
public function setPath($pValue = '', $pVerifyFile = true)
9797
{
9898
if ($pVerifyFile) {
99-
if (file_exists($pValue)) {
100-
$this->path = $pValue;
101-
102-
if ($this->width == 0 && $this->height == 0) {
103-
// Get width/height
104-
list($this->width, $this->height) = getimagesize($pValue);
105-
}
106-
} else {
99+
if (!file_exists($pValue)) {
107100
throw new \Exception("File $pValue not found!");
108101
}
102+
$this->path = $pValue;
103+
104+
if ($this->width == 0 && $this->height == 0) {
105+
// Get width/height
106+
list($this->width, $this->height) = getimagesize($pValue);
107+
}
109108
} else {
110109
$this->path = $pValue;
111110
}

src/PhpPresentation/Shape/RichText.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,12 @@ public function getParagraphs()
351351
*/
352352
public function setParagraphs($paragraphs = null)
353353
{
354-
if (is_array($paragraphs)) {
355-
$this->richTextParagraphs = $paragraphs;
356-
$this->activeParagraph = count($this->richTextParagraphs) - 1;
357-
} else {
354+
if (!is_array($paragraphs)) {
358355
throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.");
359356
}
360357

358+
$this->richTextParagraphs = $paragraphs;
359+
$this->activeParagraph = count($this->richTextParagraphs) - 1;
361360
return $this;
362361
}
363362

src/PhpPresentation/Slide/Background/Image.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ public function getPath()
4949
public function setPath($pValue = '', $pVerifyFile = true)
5050
{
5151
if ($pVerifyFile) {
52-
if (file_exists($pValue)) {
53-
$this->path = $pValue;
54-
55-
if ($this->width == 0 && $this->height == 0) {
56-
// Get width/height
57-
list($this->width, $this->height) = getimagesize($pValue);
58-
}
59-
} else {
52+
if (!file_exists($pValue)) {
6053
throw new \Exception("File $pValue not found!");
6154
}
55+
$this->path = $pValue;
56+
57+
if ($this->width == 0 && $this->height == 0) {
58+
// Get width/height
59+
list($this->width, $this->height) = getimagesize($pValue);
60+
}
61+
6262
} else {
6363
$this->path = $pValue;
6464
}

src/PhpPresentation/Style/Alignment.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class Alignment implements ComparableInterface
3939
const VERTICAL_TOP = 't';
4040
const VERTICAL_CENTER = 'ctr';
4141

42+
private $supportedStyles = array(
43+
self::HORIZONTAL_GENERAL,
44+
self::HORIZONTAL_LEFT,
45+
self::HORIZONTAL_RIGHT,
46+
);
47+
4248
/**
4349
* Horizontal
4450
*
@@ -58,28 +64,28 @@ class Alignment implements ComparableInterface
5864
*
5965
* @var int
6066
*/
61-
private $level;
67+
private $level = 0;
6268

6369
/**
6470
* Indent - only possible with horizontal alignment left and right
6571
*
6672
* @var int
6773
*/
68-
private $indent;
74+
private $indent = 0;
6975

7076
/**
7177
* Margin left - only possible with horizontal alignment left and right
7278
*
7379
* @var int
7480
*/
75-
private $marginLeft;
81+
private $marginLeft = 0;
7682

7783
/**
7884
* Margin right - only possible with horizontal alignment left and right
7985
*
8086
* @var int
8187
*/
82-
private $marginRight;
88+
private $marginRight = 0;
8389

8490
/**
8591
* Hash index
@@ -96,10 +102,6 @@ public function __construct()
96102
// Initialise values
97103
$this->horizontal = self::HORIZONTAL_LEFT;
98104
$this->vertical = self::VERTICAL_BASE;
99-
$this->level = 0;
100-
$this->indent = 0;
101-
$this->marginLeft = 0;
102-
$this->marginRight = 0;
103105
}
104106

105107
/**
@@ -199,10 +201,8 @@ public function getIndent()
199201
*/
200202
public function setIndent($pValue = 0)
201203
{
202-
if ($pValue > 0) {
203-
if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
204-
$pValue = 0; // indent not supported
205-
}
204+
if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
205+
$pValue = 0; // indent not supported
206206
}
207207

208208
$this->indent = $pValue;
@@ -228,10 +228,8 @@ public function getMarginLeft()
228228
*/
229229
public function setMarginLeft($pValue = 0)
230230
{
231-
if ($pValue > 0) {
232-
if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
233-
$pValue = 0; // margin left not supported
234-
}
231+
if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
232+
$pValue = 0; // margin left not supported
235233
}
236234

237235
$this->marginLeft = $pValue;
@@ -257,10 +255,8 @@ public function getMarginRight()
257255
*/
258256
public function setMarginRight($pValue = 0)
259257
{
260-
if ($pValue > 0) {
261-
if ($this->getHorizontal() != self::HORIZONTAL_GENERAL && $this->getHorizontal() != self::HORIZONTAL_LEFT && $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
262-
$pValue = 0; // margin left not supported
263-
}
258+
if ($pValue > 0 && !in_array($this->getHorizontal(), $this->supportedStyles)) {
259+
$pValue = 0; // margin right not supported
264260
}
265261

266262
$this->marginRight = $pValue;

src/PhpPresentation/Writer/ODPresentation.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,10 @@ public function setUseDiskCaching($pValue = false, $pDirectory = null)
165165
$this->useDiskCaching = $pValue;
166166

167167
if (!is_null($pDirectory)) {
168-
if (is_dir($pDirectory)) {
169-
$this->diskCachingDirectory = $pDirectory;
170-
} else {
168+
if (!is_dir($pDirectory)) {
171169
throw new \Exception("Directory does not exist: $pDirectory");
172170
}
171+
$this->diskCachingDirectory = $pDirectory;
173172
}
174173

175174
return $this;

0 commit comments

Comments
 (0)