Skip to content

Commit 1c3735f

Browse files
committed
Refactor table and font styles to reduce duplication
1 parent abbf60a commit 1c3735f

File tree

4 files changed

+85
-75
lines changed

4 files changed

+85
-75
lines changed

.scrutinizer.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ tools:
1717
enabled: true
1818
timeout: 900
1919
php_sim:
20-
enabled: true
2120
min_mass: 40
2221
php_pdepend: true
2322
php_analyzer: true

src/PhpWord/Style/Font.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,7 @@ public function isSuperScript()
404404
*/
405405
public function setSuperScript($value = true)
406406
{
407-
$this->superScript = $this->setBoolVal($value, $this->superScript);
408-
$this->toggleFalse($this->subScript, $this->superScript);
409-
410-
return $this;
407+
return $this->setPairedProperty($this->superScript, $this->subScript, $value);
411408
}
412409

413410
/**
@@ -428,10 +425,7 @@ public function isSubScript()
428425
*/
429426
public function setSubScript($value = true)
430427
{
431-
$this->subScript = $this->setBoolVal($value, $this->subScript);
432-
$this->toggleFalse($this->subScript, $this->superScript);
433-
434-
return $this;
428+
return $this->setPairedProperty($this->subScript, $this->superScript, $value);
435429
}
436430

437431
/**
@@ -452,10 +446,7 @@ public function isStrikethrough()
452446
*/
453447
public function setStrikethrough($value = true)
454448
{
455-
$this->strikethrough = $this->setBoolVal($value, $this->strikethrough);
456-
$this->toggleFalse($this->doubleStrikethrough, $this->strikethrough);
457-
458-
return $this;
449+
return $this->setPairedProperty($this->strikethrough, $this->doubleStrikethrough, $value);
459450
}
460451

461452
/**
@@ -476,10 +467,7 @@ public function isDoubleStrikethrough()
476467
*/
477468
public function setDoubleStrikethrough($value = true)
478469
{
479-
$this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough);
480-
$this->toggleFalse($this->strikethrough, $this->doubleStrikethrough);
481-
482-
return $this;
470+
return $this->setPairedProperty($this->doubleStrikethrough, $this->strikethrough, $value);
483471
}
484472

485473
/**
@@ -500,10 +488,7 @@ public function isSmallCaps()
500488
*/
501489
public function setSmallCaps($value = true)
502490
{
503-
$this->smallCaps = $this->setBoolVal($value, $this->smallCaps);
504-
$this->toggleFalse($this->allCaps, $this->smallCaps);
505-
506-
return $this;
491+
return $this->setPairedProperty($this->smallCaps, $this->allCaps, $value);
507492
}
508493

509494
/**
@@ -524,10 +509,7 @@ public function isAllCaps()
524509
*/
525510
public function setAllCaps($value = true)
526511
{
527-
$this->allCaps = $this->setBoolVal($value, $this->allCaps);
528-
$this->toggleFalse($this->smallCaps, $this->allCaps);
529-
530-
return $this;
512+
return $this->setPairedProperty($this->allCaps, $this->smallCaps, $value);
531513
}
532514

533515
/**
@@ -648,16 +630,21 @@ public function setShading($value = null)
648630
}
649631

650632
/**
651-
* Toggle $target property to false when $source true
633+
* Set $property value and set $pairProperty = false when $value = true
652634
*
653-
* @param bool|null $target Target property
654-
* @param bool $sourceValue
635+
* @param bool $property
636+
* @param bool $pair
637+
* @param bool $value
638+
* @return self
655639
*/
656-
private function toggleFalse(&$target, $sourceValue)
640+
private function setPairedProperty(&$property, &$pairProperty, $value)
657641
{
658-
if ($sourceValue == true) {
659-
$target = false;
642+
$property = $this->setBoolVal($value, $property);
643+
if ($value == true) {
644+
$pairProperty = false;
660645
}
646+
647+
return $this;
661648
}
662649

663650
/**

src/PhpWord/Style/Table.php

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public function __construct($tableStyle = null, $firstRowStyle = null)
124124
{
125125
$this->alignment = new Alignment();
126126

127-
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
127+
// Clone first row from table style, but with certain properties disabled
128+
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
128129
$this->firstRow = clone $this;
129130
unset($this->firstRow->firstRow);
130131
unset($this->firstRow->borderInsideHSize);
@@ -257,7 +258,7 @@ public function setBorderColor($value = null)
257258
*/
258259
public function getBorderInsideHSize()
259260
{
260-
return isset($this->borderInsideHSize) ? $this->borderInsideHSize : null;
261+
return $this->getTableOnlyProperty('borderInsideHSize');
261262
}
262263

263264
/**
@@ -268,9 +269,7 @@ public function getBorderInsideHSize()
268269
*/
269270
public function setBorderInsideHSize($value = null)
270271
{
271-
$this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize);
272-
273-
return $this;
272+
return $this->setTableOnlyProperty('borderInsideHSize', $value);
274273
}
275274

276275
/**
@@ -280,7 +279,7 @@ public function setBorderInsideHSize($value = null)
280279
*/
281280
public function getBorderInsideHColor()
282281
{
283-
return isset($this->borderInsideHColor) ? $this->borderInsideHColor : null;
282+
return $this->getTableOnlyProperty('borderInsideHColor');
284283
}
285284

286285
/**
@@ -291,9 +290,7 @@ public function getBorderInsideHColor()
291290
*/
292291
public function setBorderInsideHColor($value = null)
293292
{
294-
$this->borderInsideHColor = $value ;
295-
296-
return $this;
293+
return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
297294
}
298295

299296
/**
@@ -303,7 +300,7 @@ public function setBorderInsideHColor($value = null)
303300
*/
304301
public function getBorderInsideVSize()
305302
{
306-
return isset($this->borderInsideVSize) ? $this->borderInsideVSize : null;
303+
return $this->getTableOnlyProperty('borderInsideVSize');
307304
}
308305

309306
/**
@@ -314,9 +311,7 @@ public function getBorderInsideVSize()
314311
*/
315312
public function setBorderInsideVSize($value = null)
316313
{
317-
$this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize);
318-
319-
return $this;
314+
return $this->setTableOnlyProperty('borderInsideVSize', $value);
320315
}
321316

322317
/**
@@ -326,7 +321,7 @@ public function setBorderInsideVSize($value = null)
326321
*/
327322
public function getBorderInsideVColor()
328323
{
329-
return isset($this->borderInsideVColor) ? $this->borderInsideVColor : null;
324+
return $this->getTableOnlyProperty('borderInsideVColor');
330325
}
331326

332327
/**
@@ -337,9 +332,7 @@ public function getBorderInsideVColor()
337332
*/
338333
public function setBorderInsideVColor($value = null)
339334
{
340-
$this->borderInsideVColor = $value;
341-
342-
return $this;
335+
return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
343336
}
344337

345338
/**
@@ -349,7 +342,7 @@ public function setBorderInsideVColor($value = null)
349342
*/
350343
public function getCellMarginTop()
351344
{
352-
return $this->cellMarginTop;
345+
return $this->getTableOnlyProperty('cellMarginTop');
353346
}
354347

355348
/**
@@ -360,9 +353,7 @@ public function getCellMarginTop()
360353
*/
361354
public function setCellMarginTop($value = null)
362355
{
363-
$this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop);
364-
365-
return $this;
356+
return $this->setTableOnlyProperty('cellMarginTop', $value);
366357
}
367358

368359
/**
@@ -372,7 +363,7 @@ public function setCellMarginTop($value = null)
372363
*/
373364
public function getCellMarginLeft()
374365
{
375-
return $this->cellMarginLeft;
366+
return $this->getTableOnlyProperty('cellMarginLeft');
376367
}
377368

378369
/**
@@ -383,9 +374,7 @@ public function getCellMarginLeft()
383374
*/
384375
public function setCellMarginLeft($value = null)
385376
{
386-
$this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft);
387-
388-
return $this;
377+
return $this->setTableOnlyProperty('cellMarginLeft', $value);
389378
}
390379

391380
/**
@@ -395,7 +384,7 @@ public function setCellMarginLeft($value = null)
395384
*/
396385
public function getCellMarginRight()
397386
{
398-
return $this->cellMarginRight;
387+
return $this->getTableOnlyProperty('cellMarginRight');
399388
}
400389

401390
/**
@@ -406,9 +395,7 @@ public function getCellMarginRight()
406395
*/
407396
public function setCellMarginRight($value = null)
408397
{
409-
$this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight);
410-
411-
return $this;
398+
return $this->setTableOnlyProperty('cellMarginRight', $value);
412399
}
413400

414401
/**
@@ -418,7 +405,7 @@ public function setCellMarginRight($value = null)
418405
*/
419406
public function getCellMarginBottom()
420407
{
421-
return $this->cellMarginBottom;
408+
return $this->getTableOnlyProperty('cellMarginBottom');
422409
}
423410

424411
/**
@@ -429,9 +416,7 @@ public function getCellMarginBottom()
429416
*/
430417
public function setCellMarginBottom($value = null)
431418
{
432-
$this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom);
433-
434-
return $this;
419+
return $this->setTableOnlyProperty('cellMarginBottom', $value);
435420
}
436421

437422
/**
@@ -569,4 +554,46 @@ public function setUnit($value = null)
569554

570555
return $this;
571556
}
557+
558+
/**
559+
* Get table style only property by checking if firstRow is set
560+
*
561+
* This is necessary since firstRow style is cloned from table style but
562+
* without certain properties activated, e.g. margins
563+
*
564+
* @param string $property
565+
* @return int|string|null
566+
*/
567+
private function getTableOnlyProperty($property)
568+
{
569+
if (isset($this->firstRow)) {
570+
return $this->$property;
571+
}
572+
573+
return null;
574+
}
575+
576+
/**
577+
* Set table style only property by checking if firstRow is set
578+
*
579+
* This is necessary since firstRow style is cloned from table style but
580+
* without certain properties activated, e.g. margins
581+
*
582+
* @param string $property
583+
* @param int|string $value
584+
* @param bool $isNumeric
585+
* @return self
586+
*/
587+
private function setTableOnlyProperty($property, $value, $isNumeric = true)
588+
{
589+
if (isset($this->firstRow)) {
590+
if ($isNumeric) {
591+
$this->$property = $this->setNumericVal($value, $this->$property);
592+
} else {
593+
$this->$property = $value;
594+
}
595+
}
596+
597+
return $this;
598+
}
572599
}

tests/PhpWord/Tests/Writer/Word2007/Part/StylesTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,19 @@ public function testWriteStyles()
4949
$rStyle = array('size' => 20);
5050
$tStyle = array(
5151
'bgColor' => 'FF0000',
52-
'cellMarginTop' => 120,
53-
'cellMarginBottom' => 120,
54-
'cellMarginLeft' => 120,
55-
'cellMarginRight' => 120,
56-
'borderTopSize' => 120,
57-
'borderBottomSize' => 120,
58-
'borderLeftSize' => 120,
59-
'borderRightSize' => 120,
60-
'borderInsideHSize' => 120,
61-
'borderInsideVSize' => 120,
52+
'cellMargin' => 120,
53+
'borderSize' => 120,
54+
);
55+
$firstRowStyle = array(
56+
'bgColor' => '0000FF',
57+
'borderSize' => 120,
58+
'borderColor' => '00FF00',
6259
);
6360
$phpWord->setDefaultParagraphStyle($pStyle);
6461
$phpWord->addParagraphStyle('Base Style', $pBase);
6562
$phpWord->addParagraphStyle('New Style', $pNew);
6663
$phpWord->addFontStyle('New Style', $rStyle, $pStyle);
67-
$phpWord->addTableStyle('Table Style', $tStyle, $tStyle);
64+
$phpWord->addTableStyle('Table Style', $tStyle, $firstRowStyle);
6865
$phpWord->addTitleStyle(1, $rStyle, $pStyle);
6966
$doc = TestHelperDOCX::getDocument($phpWord);
7067

0 commit comments

Comments
 (0)