Completed
Pull Request — develop (#230)
by Franck
07:05
created

TextStyle   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.47%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 254
ccs 40
cts 53
cp 0.7547
rs 10
c 2
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A checkLvl() 0 10 3
A setBodyStyleAtLvl() 0 6 2
A setTitleStyleAtLvl() 0 6 2
A setOtherStyleAtLvl() 0 6 2
A getBodyStyleAtLvl() 0 6 2
A getTitleStyleAtLvl() 0 6 2
A getOtherStyleAtLvl() 0 6 2
A getBodyStyle() 0 4 1
A getTitleStyle() 0 4 1
A getOtherStyle() 0 4 1
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Style;
19
20
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph as RichTextParagraph;
21
22
/**
23
 * Class TextStyle
24
 */
25
class TextStyle
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $bodyStyle = array();
31
    /**
32
     * @var array
33
     */
34
    protected $titleStyle = array();
35
    /**
36
     * @var array
37
     */
38
    protected $otherStyle = array();
39
40
    /**
41
     * TextStyle constructor.
42
     * @param bool $default
43
     */
44 191
    public function __construct($default = true)
45
    {
46 191
        if ($default) {
47 191
            $oRTParagraph = new RichTextParagraph();
48 191
            $oRTParagraph->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
49 191
            $oRTParagraph->getFont()->setSize(44)->setColor(new SchemeColor())->getColor()->setValue("lt1");
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpOffice\PhpPresentation\Style\Color as the method setValue() does only exist in the following sub-classes of PhpOffice\PhpPresentation\Style\Color: PhpOffice\PhpPresentation\Style\SchemeColor. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
50 191
            $this->titleStyle[1] = $oRTParagraph;
51 191
            $oRTParagraph = new RichTextParagraph();
52 191
            $oRTParagraph->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)
53 191
                ->setIndent(-324900 / 9525)
54 191
                ->setMarginLeft(342900 / 9525);
55 191
            $oRTParagraph->getFont()->setSize(32)->setColor(new SchemeColor())->getColor()->setValue("tx1");
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpOffice\PhpPresentation\Style\Color as the method setValue() does only exist in the following sub-classes of PhpOffice\PhpPresentation\Style\Color: PhpOffice\PhpPresentation\Style\SchemeColor. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56 191
            $this->bodyStyle[1] = $oRTParagraph;
57 191
            $oRTParagraph = new RichTextParagraph();
58 191
            $oRTParagraph->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
59 191
            $oRTParagraph->getFont()->setSize(10)->setColor(new SchemeColor())->getColor()->setValue("tx1");
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpOffice\PhpPresentation\Style\Color as the method setValue() does only exist in the following sub-classes of PhpOffice\PhpPresentation\Style\Color: PhpOffice\PhpPresentation\Style\SchemeColor. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
60 191
            $this->otherStyle[0] = $oRTParagraph;
61
        }
62 191
    }
63
64
    /**
65
     * @param $lvl
66
     * @return bool
67
     */
68 3
    private function checkLvl($lvl)
69
    {
70 3
        if (!is_int($lvl)) {
71 3
            return false;
72
        }
73 3
        if ($lvl > 9) {
74
            return false;
75
        }
76 3
        return true;
77
    }
78
79
    /**
80
     * @param RichTextParagraph $style
81
     * @param $lvl
82
     */
83 3
    public function setBodyStyleAtLvl(RichTextParagraph $style, $lvl)
84
    {
85 3
        if (!$this->checkLvl($lvl)) {
86 3
            $this->bodyStyle[$lvl] = $style;
87
        }
88 3
    }
89
90
    /**
91
     * @param RichTextParagraph $style
92
     * @param $lvl
93
     */
94 3
    public function setTitleStyleAtLvl(RichTextParagraph $style, $lvl)
95
    {
96 3
        if (!$this->checkLvl($lvl)) {
97 3
            $this->titleStyle[$lvl] = $style;
98
        }
99 3
    }
100
101
    /**
102
     * @param RichTextParagraph $style
103
     * @param $lvl
104
     */
105 3
    public function setOtherStyleAtLvl(RichTextParagraph $style, $lvl)
106
    {
107 3
        if (!$this->checkLvl($lvl)) {
108 3
            $this->otherStyle[$lvl] = $style;
109
        }
110 3
    }
111
112
    /**
113
     * @param $lvl
114
     * @return mixed
115
     */
116
    public function getBodyStyleAtLvl($lvl)
117
    {
118
        if (!$this->checkLvl($lvl)) {
119
            return $this->bodyStyle[$lvl];
120
        }
121
    }
122
123
    /**
124
     * @param $lvl
125
     * @return mixed
126
     */
127
    public function getTitleStyleAtLvl($lvl)
128
    {
129
        if (!$this->checkLvl($lvl)) {
130
            return $this->bodyStyle[$lvl];
131
        }
132
    }
133
134
    /**
135
     * @param $lvl
136
     * @return mixed
137
     */
138
    public function getOtherStyleAtLvl($lvl)
139
    {
140
        if (!$this->checkLvl($lvl)) {
141
            return $this->bodyStyle[$lvl];
142
        }
143
    }
144
145
    /**
146
     * @return array
147
     */
148 95
    public function getBodyStyle()
149
    {
150 95
        return $this->bodyStyle;
151
    }
152
153
    /**
154
     * @return array
155
     */
156 95
    public function getTitleStyle()
157
    {
158 95
        return $this->titleStyle;
159
    }
160
161
    /**
162
     * @return array
163
     */
164 95
    public function getOtherStyle()
165
    {
166 95
        return $this->otherStyle;
167
    }
168
//
169
//
170
//
171
//
172
//
173
//
174
//
175
//
176
//
177
//
178
//
179
//<p:titleStyle>
180
//    <a:lvl1pPr algn="ctr" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
181
//        <a:spcBef>
182
//            <a:spcPct val="0" />
183
//        </a:spcBef>
184
//        <a:buNone />
185
//        <a:defRPr sz="4400" kern="1200">
186
//            <a:solidFill>
187
//                <a:schemeClr val="tx1" />
188
//            </a:solidFill>
189
//            <a:latin typeface="+mj-lt" />
190
//            <a:ea typeface="+mj-ea" />
191
//            <a:cs typeface="+mj-cs" />
192
//        </a:defRPr>
193
//    </a:lvl1pPr>
194
//</p:titleStyle>
195
//
196
//
197
//    // Create a shape (text)
198
//echo date('H:i:s') . ' Create a shape (rich text)'.EOL;
199
//$shape = $currentSlide->createRichTextShape();
200
//$shape->setHeight(600)
201
//->setWidth(930)
202
//->setOffsetX(10)
203
//->setOffsetY(130);
204
//$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)
205
//->setMarginLeft(25)
206
//->setIndent(-25);
207
//$shape->getActiveParagraph()->getFont()->setSize(36)
208
//->setColor($colorBlack);
209
//$shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET);
210
//
211
//$shape->createTextRun('Generate slide decks');
212
//
213
//$shape->createParagraph()->getAlignment()->setLevel(1)
214
//->setMarginLeft(75)
215
//->setIndent(-25);
216
//$shape->createTextRun('Represent business data');
217
//$shape->createParagraph()->createTextRun('Show a family slide show');
218
//$shape->createParagraph()->createTextRun('...');
219
//
220
//$shape->createParagraph()->getAlignment()->setLevel(0)
221
//->setMarginLeft(25)
222
//->setIndent(-25);
223
//$shape->createTextRun('Export these to different formats');
224
//$shape->createParagraph()->getAlignment()->setLevel(1)
225
//->setMarginLeft(75)
226
//->setIndent(-25);
227
//$shape->createTextRun('PHPPresentation 2007');
228
//$shape->createParagraph()->createTextRun('Serialized');
229
//$shape->createParagraph()->createTextRun('... (more to come) ...');
230
//
231
    /**
232
     * Text Styles
233
     * p:bodyStyle
234
     * p:titleStyle
235
     * p:otherStyle
236
     *
237
     * a:defPPr
238
     * a:lvl1pPr
239
     * .
240
     * .
241
     * .
242
     * a:lvl9pPr
243
     * attributes
244
     * - algn (alignment)
245
     * - defTabSz (default tab size)
246
     * - fontAlgn (font alignment)
247
     * - hangingPunct (specifies the handling of hanging text)
248
     * - indent (indentation for the first line of text)
249
     * - latinLnBrk (specifies whether to break words)
250
     * - marL and marR (left and right margins)
251
     * - lang
252
     *elements*
253
     *
254
     * Bullets & Numbering (http://officeopenxml.com/drwSp-text-paraProps-numbering.php)
255
     * <a:buAutoNum/> (auto-numbering bullet)
256
     * <a:buBlip> (picture bullet)
257
     * <a:buChar/> (character bullet)
258
     * <a:buClr> (color of bullets)
259
     * <a:buClrTx> (color of bullets is same as text run)
260
     * <a:buFont/> (font for bullets)
261
     * <a:buFontTx> (font for bullets is same as text run)
262
     * <a:buNone> (no bullet)
263
     * <a:buSzPct> (size in percentage of bullet characters)
264
     * <a:buSzPts/> (size in points of bullet characters)
265
     * <a:buSzTx> (size of bullet characters to be size of text run)
266
     *
267
     * List Properties and Default Style. (http://officeopenxml.com/drwSp-text-lstPr.php)
268
     * <a:defRPr> (default text run properties)
269
     *
270
     * Text - Spacing, Indents and Margins. (http://officeopenxml.com/drwSp-text-paraProps-margins.php)
271
     * <a:lnSpc> (line spacing)
272
     * <a:spcAft> (spacing after the paragraph)
273
     * <a:spcBef> (spacing before the paragraph)
274
     *
275
     * Text - Alignment, Tabs, Other (http://officeopenxml.com/drwSp-text-paraProps-align.php)
276
     * <a:tabLst> (list of tab stops in a paragraph)
277
     */
278
}
279