Skip to content

Commit e55fbb5

Browse files
authored
Merge pull request #281 from oliverklee/cleanup/types-declarationblock
Add type annotations for `DeclarationBlock`
2 parents 70a01c9 + 276e569 commit e55fbb5

File tree

1 file changed

+94
-25
lines changed

1 file changed

+94
-25
lines changed

src/RuleSet/DeclarationBlock.php

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Sabberworm\CSS\RuleSet;
44

5+
use Sabberworm\CSS\CSSList\CSSList;
56
use Sabberworm\CSS\CSSList\KeyFrame;
67
use Sabberworm\CSS\OutputFormat;
78
use Sabberworm\CSS\Parsing\OutputException;
89
use Sabberworm\CSS\Parsing\ParserState;
10+
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
911
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1012
use Sabberworm\CSS\Property\KeyframeSelector;
1113
use Sabberworm\CSS\Property\Selector;
@@ -17,22 +19,34 @@
1719
use Sabberworm\CSS\Value\Value;
1820

1921
/**
20-
* Declaration blocks are the parts of a css file which denote the rules belonging to a selector.
21-
* Declaration blocks usually appear directly inside a Document or another CSSList (mostly a MediaQuery).
22+
* Declaration blocks are the parts of a CSS file which denote the rules belonging to a selector.
23+
*
24+
* Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
2225
*/
2326
class DeclarationBlock extends RuleSet
2427
{
2528
/**
26-
* @var array<int, Selector>
29+
* @var array<int, Selector|string>
2730
*/
2831
private $aSelectors;
2932

33+
/**
34+
* @param int $iLineNo
35+
*/
3036
public function __construct($iLineNo = 0)
3137
{
3238
parent::__construct($iLineNo);
3339
$this->aSelectors = [];
3440
}
3541

42+
/**
43+
* @param CSSList|null $oList
44+
*
45+
* @return DeclarationBlock|false
46+
*
47+
* @throws UnexpectedTokenException
48+
* @throws UnexpectedEOFException
49+
*/
3650
public static function parse(ParserState $oParserState, $oList = null)
3751
{
3852
$aComments = [];
@@ -70,6 +84,12 @@ public static function parse(ParserState $oParserState, $oList = null)
7084
return $oResult;
7185
}
7286

87+
/**
88+
* @param array<int, Selector|string>|string $mSelector
89+
* @param CSSList|null $oList
90+
*
91+
* @throws UnexpectedTokenException
92+
*/
7393
public function setSelectors($mSelector, $oList = null)
7494
{
7595
if (is_array($mSelector)) {
@@ -104,6 +124,10 @@ public function setSelectors($mSelector, $oList = null)
104124

105125
/**
106126
* Remove one of the selectors of the block.
127+
*
128+
* @param Selector|string $mSelector
129+
*
130+
* @return bool
107131
*/
108132
public function removeSelector($mSelector)
109133
{
@@ -120,6 +144,8 @@ public function removeSelector($mSelector)
120144
}
121145

122146
/**
147+
* @return array<int, Selector|string>
148+
*
123149
* @deprecated use `getSelectors()`
124150
*/
125151
public function getSelector()
@@ -128,6 +154,11 @@ public function getSelector()
128154
}
129155

130156
/**
157+
* @param Selector|string $mSelector
158+
* @param CSSList|null $oList
159+
*
160+
* @return void
161+
*
131162
* @deprecated use `setSelectors()`
132163
*/
133164
public function setSelector($mSelector, $oList = null)
@@ -136,18 +167,18 @@ public function setSelector($mSelector, $oList = null)
136167
}
137168

138169
/**
139-
* Get selectors.
140-
*
141-
* @return array<int, Selector> Selectors.
170+
* @return array<int, Selector|string>
142171
*/
143172
public function getSelectors()
144173
{
145174
return $this->aSelectors;
146175
}
147176

148177
/**
149-
* Split shorthand declarations (e.g. +margin+ or +font+) into their constituent parts.
150-
* */
178+
* Splits shorthand declarations (e.g. `margin` or `font`) into their constituent parts.
179+
*
180+
* @return void
181+
*/
151182
public function expandShorthands()
152183
{
153184
// border must be expanded before dimensions
@@ -159,8 +190,10 @@ public function expandShorthands()
159190
}
160191

161192
/**
162-
* Create shorthand declarations (e.g. +margin+ or +font+) whenever possible.
163-
* */
193+
* Creates shorthand declarations (e.g. `margin` or `font`) whenever possible.
194+
*
195+
* @return void
196+
*/
164197
public function createShorthands()
165198
{
166199
$this->createBackgroundShorthand();
@@ -172,10 +205,14 @@ public function createShorthands()
172205
}
173206

174207
/**
175-
* Split shorthand border declarations (e.g. <tt>border: 1px red;</tt>)
176-
* Additional splitting happens in expandDimensionsShorthand
177-
* Multiple borders are not yet supported as of 3
178-
* */
208+
* Splits shorthand border declarations (e.g. `border: 1px red;`).
209+
*
210+
* Additional splitting happens in expandDimensionsShorthand.
211+
*
212+
* Multiple borders are not yet supported as of 3.
213+
*
214+
* @return void
215+
*/
179216
public function expandBorderShorthand()
180217
{
181218
$aBorderRules = [
@@ -230,10 +267,13 @@ public function expandBorderShorthand()
230267
}
231268

232269
/**
233-
* Split shorthand dimensional declarations (e.g. <tt>margin: 0px auto;</tt>)
270+
* Splits shorthand dimensional declarations (e.g. `margin: 0px auto;`)
234271
* into their constituent parts.
235-
* Handles margin, padding, border-color, border-style and border-width.
236-
* */
272+
*
273+
* Handles `margin`, `padding`, `border-color`, `border-style` and `border-width`.
274+
*
275+
* @return void
276+
*/
237277
public function expandDimensionsShorthand()
238278
{
239279
$aExpansions = [
@@ -288,10 +328,12 @@ public function expandDimensionsShorthand()
288328
}
289329

290330
/**
291-
* Convert shorthand font declarations
331+
* Converts shorthand font declarations
292332
* (e.g. `font: 300 italic 11px/14px verdana, helvetica, sans-serif;`)
293333
* into their constituent parts.
294-
* */
334+
*
335+
* @return void
336+
*/
295337
public function expandFontShorthand()
296338
{
297339
$aRules = $this->getRulesAssoc();
@@ -359,6 +401,8 @@ public function expandFontShorthand()
359401
* into their constituent parts.
360402
*
361403
* @see http://www.w3.org/TR/21/colors.html#propdef-background
404+
*
405+
* @return void
362406
*/
363407
public function expandBackgroundShorthand()
364408
{
@@ -429,6 +473,9 @@ public function expandBackgroundShorthand()
429473
$this->removeRule('background');
430474
}
431475

476+
/**
477+
* @return void
478+
*/
432479
public function expandListStyleShorthand()
433480
{
434481
$aListProperties = [
@@ -506,6 +553,12 @@ public function expandListStyleShorthand()
506553
$this->removeRule('list-style');
507554
}
508555

556+
/**
557+
* @param array<array-key, string> $aProperties
558+
* @param string $sShorthand
559+
*
560+
* @return void
561+
*/
509562
public function createShorthandProperties(array $aProperties, $sShorthand)
510563
{
511564
$aRules = $this->getRulesAssoc();
@@ -538,6 +591,9 @@ public function createShorthandProperties(array $aProperties, $sShorthand)
538591
}
539592
}
540593

594+
/**
595+
* @return void
596+
*/
541597
public function createBackgroundShorthand()
542598
{
543599
$aProperties = [
@@ -550,6 +606,9 @@ public function createBackgroundShorthand()
550606
$this->createShorthandProperties($aProperties, 'background');
551607
}
552608

609+
/**
610+
* @return void
611+
*/
553612
public function createListStyleShorthand()
554613
{
555614
$aProperties = [
@@ -561,9 +620,12 @@ public function createListStyleShorthand()
561620
}
562621

563622
/**
564-
* Combine border-color, border-style and border-width into border
565-
* Should be run after create_dimensions_shorthand!
566-
* */
623+
* Combines `border-color`, `border-style` and `border-width` into `border`.
624+
*
625+
* Should be run after `create_dimensions_shorthand`!
626+
*
627+
* @return void
628+
*/
567629
public function createBorderShorthand()
568630
{
569631
$aProperties = [
@@ -578,6 +640,8 @@ public function createBorderShorthand()
578640
* Looks for long format CSS dimensional properties
579641
* (margin, padding, border-color, border-style and border-width)
580642
* and converts them into shorthand CSS properties.
643+
*
644+
* @return void
581645
*/
582646
public function createDimensionsShorthand()
583647
{
@@ -649,7 +713,9 @@ public function createDimensionsShorthand()
649713
* Looks for long format CSS font properties (e.g. `font-weight`) and
650714
* tries to convert them into a shorthand CSS `font` property.
651715
*
652-
* At least font-size AND font-family must be present in order to create a shorthand declaration.
716+
* At least `font-size` AND `font-family` must be present in order to create a shorthand declaration.
717+
*
718+
* @return void
653719
*/
654720
public function createFontShorthand()
655721
{
@@ -729,14 +795,17 @@ public function createFontShorthand()
729795
}
730796
}
731797

798+
/**
799+
* @return string
800+
*
801+
* @throws OutputException
802+
*/
732803
public function __toString()
733804
{
734805
return $this->render(new OutputFormat());
735806
}
736807

737808
/**
738-
* @param OutputFormat $oOutputFormat
739-
*
740809
* @return string
741810
*
742811
* @throws OutputException

0 commit comments

Comments
 (0)