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

TextStyle::getTitleStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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