Skip to content

Commit 1ed8f0d

Browse files
committed
Refactor: Remove some duplication found by phpcpd. One more to go.
1 parent b4c2298 commit 1ed8f0d

File tree

9 files changed

+381
-943
lines changed

9 files changed

+381
-943
lines changed

src/PhpWord/Element/CheckBox.php

Lines changed: 6 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99

1010
namespace PhpOffice\PhpWord\Element;
1111

12-
use PhpOffice\PhpWord\Style\Font;
13-
use PhpOffice\PhpWord\Style\Paragraph;
14-
1512
/**
1613
* Check box element
1714
*/
18-
class CheckBox extends AbstractElement
15+
class CheckBox extends Text
1916
{
2017
/**
2118
* Name content
@@ -25,118 +22,30 @@ class CheckBox extends AbstractElement
2522
private $name;
2623

2724
/**
28-
* Text content
29-
*
30-
* @var string
31-
*/
32-
private $text;
33-
34-
/**
35-
* Text style
36-
*
37-
* @var string|\PhpOffice\PhpWord\Style\Font
38-
*/
39-
private $fontStyle;
40-
41-
/**
42-
* Paragraph style
43-
*
44-
* @var string|\PhpOffice\PhpWord\Style\Paragraph
45-
*/
46-
private $paragraphStyle;
47-
48-
/**
49-
* Create a new Text Element
25+
* Create new instance
5026
*
5127
* @param string $name
5228
* @param string $text
5329
* @param mixed $fontStyle
5430
* @param mixed $paragraphStyle
31+
* @return self
5532
*/
5633
public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null)
5734
{
5835
$this->setName($name);
59-
$this->setText($text);
60-
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
61-
$this->setFontStyle($fontStyle, $paragraphStyle);
62-
63-
return $this;
64-
}
65-
66-
/**
67-
* Set Text style
68-
*
69-
* @param mixed $style
70-
* @param mixed $paragraphStyle
71-
* @return string|\PhpOffice\PhpWord\Style\Font
72-
*/
73-
public function setFontStyle($style = null, $paragraphStyle = null)
74-
{
75-
if ($style instanceof Font) {
76-
$this->fontStyle = $style;
77-
$this->setParagraphStyle($paragraphStyle);
78-
} elseif (is_array($style)) {
79-
$this->fontStyle = new Font('text', $paragraphStyle);
80-
$this->fontStyle->setArrayStyle($style);
81-
} elseif (null === $style) {
82-
$this->fontStyle = new Font('text', $paragraphStyle);
83-
} else {
84-
$this->fontStyle = $style;
85-
$this->setParagraphStyle($paragraphStyle);
86-
}
87-
return $this->fontStyle;
88-
}
89-
90-
/**
91-
* Get Text style
92-
*
93-
* @return string|\PhpOffice\PhpWord\Style\Font
94-
*/
95-
public function getFontStyle()
96-
{
97-
return $this->fontStyle;
98-
}
99-
100-
/**
101-
* Set Paragraph style
102-
*
103-
* @param mixed $style
104-
* @return string|\PhpOffice\PhpWord\Style\Paragraph
105-
*/
106-
public function setParagraphStyle($style = null)
107-
{
108-
if (is_array($style)) {
109-
$this->paragraphStyle = new Paragraph;
110-
$this->paragraphStyle->setArrayStyle($style);
111-
} elseif ($style instanceof Paragraph) {
112-
$this->paragraphStyle = $style;
113-
} elseif (null === $style) {
114-
$this->paragraphStyle = new Paragraph;
115-
} else {
116-
$this->paragraphStyle = $style;
117-
}
118-
return $this->paragraphStyle;
119-
}
120-
121-
/**
122-
* Get Paragraph style
123-
*
124-
* @return string|\PhpOffice\PhpWord\Style\Paragraph
125-
*/
126-
public function getParagraphStyle()
127-
{
128-
return $this->paragraphStyle;
36+
parent::__construct($text, $fontStyle, $paragraphStyle);
12937
}
13038

13139
/**
13240
* Set name content
13341
*
13442
* @param string $name
135-
* @return $this
43+
* @return self
13644
*/
13745
public function setName($name)
13846
{
13947
$this->name = $name;
48+
14049
return $this;
14150
}
14251

@@ -149,26 +58,4 @@ public function getName()
14958
{
15059
return $this->name;
15160
}
152-
153-
/**
154-
* Set text content
155-
*
156-
* @param string $text
157-
* @return $this
158-
*/
159-
public function setText($text)
160-
{
161-
$this->text = $text;
162-
return $this;
163-
}
164-
165-
/**
166-
* Get text content
167-
*
168-
* @return string
169-
*/
170-
public function getText()
171-
{
172-
return $this->text;
173-
}
17461
}

src/PhpWord/Element/Link.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function getParagraphStyle()
109109
*
110110
* @return string
111111
* @deprecated 0.10.0
112+
* @codeCoverageIgnore
112113
*/
113114
public function getLinkSrc()
114115
{
@@ -120,6 +121,7 @@ public function getLinkSrc()
120121
*
121122
* @return string
122123
* @deprecated 0.10.0
124+
* @codeCoverageIgnore
123125
*/
124126
public function getLinkName()
125127
{

src/PhpWord/Element/Text.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ class Text extends AbstractElement
2222
*
2323
* @var string
2424
*/
25-
private $text;
25+
protected $text;
2626

2727
/**
2828
* Text style
2929
*
3030
* @var string|\PhpOffice\PhpWord\Style\Font
3131
*/
32-
private $fontStyle;
32+
protected $fontStyle;
3333

3434
/**
3535
* Paragraph style
3636
*
3737
* @var string|\PhpOffice\PhpWord\Style\Paragraph
3838
*/
39-
private $paragraphStyle;
39+
protected $paragraphStyle;
4040

4141
/**
4242
* Create a new Text Element
@@ -73,6 +73,7 @@ public function setFontStyle($style = null, $paragraphStyle = null)
7373
$this->fontStyle = $style;
7474
$this->setParagraphStyle($paragraphStyle);
7575
}
76+
7677
return $this->fontStyle;
7778
}
7879

@@ -104,6 +105,7 @@ public function setParagraphStyle($style = null)
104105
} else {
105106
$this->paragraphStyle = $style;
106107
}
108+
107109
return $this->paragraphStyle;
108110
}
109111

@@ -121,11 +123,12 @@ public function getParagraphStyle()
121123
* Set text content
122124
*
123125
* @param string $text
124-
* @return $this
126+
* @return self
125127
*/
126128
public function setText($text)
127129
{
128130
$this->text = $text;
131+
129132
return $this;
130133
}
131134

src/PhpWord/Style/AbstractStyle.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ public function setStyleByArray($styles = array())
117117
return $this;
118118
}
119119

120+
/**
121+
* Set boolean value
122+
*
123+
* @param mixed $value
124+
* @param mixed $default
125+
* @return mixed
126+
*/
127+
protected function setNonEmptyVal($value, $default)
128+
{
129+
if (is_null($value) || $value == '') {
130+
$value = $default;
131+
}
132+
133+
return $value;
134+
}
135+
120136
/**
121137
* Set boolean value
122138
*
@@ -133,6 +149,22 @@ protected function setBoolVal($value, $default = null)
133149
return $value;
134150
}
135151

152+
/**
153+
* Set numeric value
154+
*
155+
* @param mixed $value
156+
* @param integer|float|null $default
157+
* @return integer|float|null
158+
*/
159+
protected function setNumericVal($value, $default = null)
160+
{
161+
if (!is_numeric($value)) {
162+
$value = $default;
163+
}
164+
165+
return $value;
166+
}
167+
136168
/**
137169
* Set integer value
138170
*

0 commit comments

Comments
 (0)