Skip to content

Add some PHPDoc type annotations #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@
* A BlockList constructed by an unknown @-rule. @media rules are rendered into AtRuleBlockList objects.
*/
class AtRuleBlockList extends CSSBlockList implements AtRule {

/**
* @var string
*/
private $sType;

/**
* @var string
*/
private $sArgs;

/**
* @param string $sType
* @param string $sArgs
* @param int $iLineNo
*/
public function __construct($sType, $sArgs = '', $iLineNo = 0) {
parent::__construct($iLineNo);
$this->sType = $sType;
$this->sArgs = $sArgs;
}

/**
* @return string
*/
public function atRuleName() {
return $this->sType;
}

/**
* @return string
*/
public function atRuleArgs() {
return $this->sArgs;
}
Expand All @@ -30,6 +47,9 @@ public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

/**
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sArgs = $this->sArgs;
if($sArgs) {
Expand Down
30 changes: 25 additions & 5 deletions lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@
* Also, it may contain Import and Charset objects stemming from @-rules.
*/
abstract class CSSList implements Renderable, Commentable {

/**
* @var array
*/
protected $aComments;

/**
* @var array<int, RuleSet|Import|Charset|CSSList>
*/
protected $aContents;

/**
* @var int
*/
protected $iLineNo;

/**
* @param int $iLineNo
*/
public function __construct($iLineNo = 0) {
$this->aComments = array();
$this->aContents = array();
Expand Down Expand Up @@ -195,7 +208,7 @@ public function getLineNo() {
/**
* Prepend item to list of contents.
*
* @param object $oItem Item.
* @param RuleSet|Import|Charset|CSSList $oItem Item.
*/
public function prepend($oItem) {
array_unshift($this->aContents, $oItem);
Expand All @@ -204,7 +217,7 @@ public function prepend($oItem) {
/**
* Append item to list of contents.
*
* @param object $oItem Item.
* @param RuleSet|Import|Charset|CSSList $oItem Item.
*/
public function append($oItem) {
$this->aContents[] = $oItem;
Expand Down Expand Up @@ -237,6 +250,7 @@ public function remove($oItemToRemove) {

/**
* Replaces an item from the CSS list.
*
* @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery)
*/
public function replace($oOldItem, $mNewItem) {
Expand All @@ -254,7 +268,7 @@ public function replace($oOldItem, $mNewItem) {

/**
* Set the contents.
* @param array $aContents Objects to set as content.
* @param array<int, RuleSet|Import|Charset|CSSList> $aContents Objects to set as content.
*/
public function setContents(array $aContents) {
$this->aContents = array();
Expand Down Expand Up @@ -300,6 +314,9 @@ public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

/**
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = '';
$bIsFirst = true;
Expand Down Expand Up @@ -330,12 +347,15 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {

return $sResult;
}

/**
* Return true if the list can not be further outdented. Only important when rendering.
*/
public abstract function isRootList();

/**
* @return array<int, RuleSet|Import|Charset|CSSList>
*/
public function getContents() {
return $this->aContents;
}
Expand Down
16 changes: 15 additions & 1 deletion lib/Sabberworm/CSS/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public function __construct($iLineNo = 0) {
parent::__construct($iLineNo);
}

/**
* @param ParserState $oParserState
*
* @return Document
*
* @throws \Sabberworm\CSS\Parsing\SourceException
*/
public static function parse(ParserState $oParserState) {
$oDocument = new Document($oParserState->currentLine());
CSSList::parseList($oParserState, $oDocument);
Expand Down Expand Up @@ -95,7 +102,14 @@ public function createShorthands() {
}
}

// Override render() to make format argument optional

/**
* Override render() to make format argument optional
*
* @param \Sabberworm\CSS\OutputFormat|null $oOutputFormat
*
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
Expand Down
19 changes: 18 additions & 1 deletion lib/Sabberworm/CSS/CSSList/KeyFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
use Sabberworm\CSS\Property\AtRule;

class KeyFrame extends CSSList implements AtRule {

/**
* @var string|null
*/
private $vendorKeyFrame;

/**
* @var string|null
*/
private $animationName;

public function __construct($iLineNo = 0) {
Expand Down Expand Up @@ -35,6 +41,11 @@ public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

/**
* @param \Sabberworm\CSS\OutputFormat $oOutputFormat
*
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = "@{$this->vendorKeyFrame} {$this->animationName}{$oOutputFormat->spaceBeforeOpeningBrace()}{";
$sResult .= parent::render($oOutputFormat);
Expand All @@ -46,10 +57,16 @@ public function isRootList() {
return false;
}

/**
* @return string|null
*/
public function atRuleName() {
return $this->vendorKeyFrame;
}

/**
* @return string|null
*/
public function atRuleArgs() {
return $this->animationName;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/Sabberworm/CSS/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* Parser class parses CSS from text into a data structure.
*/
class Parser {
/**
* @var ParserState
*/
private $oParserState;

/**
Expand All @@ -34,8 +37,12 @@ public function getCharset() {
$this->oParserState->getCharset();
}

/**
* @return Document
*
* @throws Parsing\SourceException
*/
public function parse() {
return Document::parse($this->oParserState);
}

}
9 changes: 8 additions & 1 deletion lib/Sabberworm/CSS/Property/AtRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ interface AtRule extends Renderable, Commentable {
const BLOCK_RULES = 'media/document/supports/region-style/font-feature-values';
// …and more font-specific ones (to be used inside font-feature-values)
const SET_RULES = 'font-face/counter-style/page/swash/styleset/annotation';


/**
* @return string|null
*/
public function atRuleName();

/**
* @return string|null
*/
public function atRuleArgs();
}
15 changes: 13 additions & 2 deletions lib/Sabberworm/CSS/Property/CSSNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CSSNamespace implements AtRule {
private $sPrefix;
private $iLineNo;
protected $aComments;

public function __construct($mUrl, $sPrefix = null, $iLineNo = 0) {
$this->mUrl = $mUrl;
$this->sPrefix = $sPrefix;
Expand All @@ -29,10 +29,15 @@ public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

/**
* @param \Sabberworm\CSS\OutputFormat $oOutputFormat
*
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return '@namespace '.($this->sPrefix === null ? '' : $this->sPrefix.' ').$this->mUrl->render($oOutputFormat).';';
}

public function getUrl() {
return $this->mUrl;
}
Expand All @@ -49,10 +54,16 @@ public function setPrefix($sPrefix) {
$this->sPrefix = $sPrefix;
}

/**
* @return string
*/
public function atRuleName() {
return 'namespace';
}

/**
* @return array<int, string>
*/
public function atRuleArgs() {
$aResult = array($this->mUrl);
if($this->sPrefix) {
Expand Down
29 changes: 27 additions & 2 deletions lib/Sabberworm/CSS/Property/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@
* • Must not appear more than once.
*/
class Charset implements AtRule {

/**
* @var string
*/
private $sCharset;

/**
* @var int
*/
protected $iLineNo;

/**
* @var array
*/
protected $aComment;

/**
* @param string $sCharset
* @param int $iLineNo
*/
public function __construct($sCharset, $iLineNo = 0) {
$this->sCharset = $sCharset;
$this->iLineNo = $iLineNo;
Expand All @@ -40,14 +54,25 @@ public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

/**
* @param \Sabberworm\CSS\OutputFormat $oOutputFormat
*
* @return string
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return "@charset {$this->sCharset->render($oOutputFormat)};";
}

/**
* @return string
*/
public function atRuleName() {
return 'charset';
}

/**
* @return string
*/
public function atRuleArgs() {
return $this->sCharset;
}
Expand All @@ -63,4 +88,4 @@ public function getComments() {
public function setComments(array $aComments) {
$this->aComments = $aComments;
}
}
}
Loading