55namespace Sabberworm \CSS \Tests ;
66
77use PHPUnit \Framework \TestCase ;
8+ use Sabberworm \CSS \Comment \Commentable ;
9+ use Sabberworm \CSS \CSSList \CSSList ;
810use Sabberworm \CSS \CSSList \Document ;
911use Sabberworm \CSS \CSSList \KeyFrame ;
1012use Sabberworm \CSS \OutputFormat ;
@@ -973,9 +975,30 @@ public function lineNumbersParsing(): void
973975
974976 $ actual = [];
975977 foreach ($ document ->getContents () as $ contentItem ) {
978+ // PHPStan can see what `assertInstanceOf()` does,
979+ // but does not understand `LogicalOr` with multiple `IsIntanceOf` constraints.
980+ // So a more explicit type check is required.
981+ // TODO: tidy this up when an interface with `getLineNo()` is added.
982+ if (
983+ !$ contentItem instanceof Charset &&
984+ !$ contentItem instanceof CSSList &&
985+ !$ contentItem instanceof CSSNamespace &&
986+ !$ contentItem instanceof Import &&
987+ !$ contentItem instanceof RuleSet
988+ ) {
989+ self ::fail ('Content item is not of an expected type. It \'s a ` ' . \get_class ($ contentItem ) . '`. ' );
990+ }
976991 $ actual [$ contentItem ->getLineNo ()] = [\get_class ($ contentItem )];
977992 if ($ contentItem instanceof KeyFrame) {
978993 foreach ($ contentItem ->getContents () as $ block ) {
994+ if (
995+ !$ block instanceof CSSList &&
996+ !$ block instanceof RuleSet
997+ ) {
998+ self ::fail (
999+ 'KeyFrame content item is not of an expected type. It \'s a ` ' . \get_class ($ block ) . '`. '
1000+ );
1001+ }
9791002 $ actual [$ contentItem ->getLineNo ()][] = $ block ->getLineNo ();
9801003 }
9811004 }
@@ -1037,37 +1060,44 @@ public function commentExtracting(): void
10371060 $ nodes = $ document ->getContents ();
10381061
10391062 // Import property.
1063+ self ::assertInstanceOf (Commentable::class, $ nodes [0 ]);
10401064 $ importComments = $ nodes [0 ]->getComments ();
10411065 self ::assertCount (2 , $ importComments );
10421066 self ::assertSame ("* \n * Comments \n " , $ importComments [0 ]->getComment ());
10431067 self ::assertSame (' Hell ' , $ importComments [1 ]->getComment ());
10441068
10451069 // Declaration block.
10461070 $ fooBarBlock = $ nodes [1 ];
1071+ self ::assertInstanceOf (Commentable::class, $ fooBarBlock );
10471072 $ fooBarBlockComments = $ fooBarBlock ->getComments ();
10481073 // TODO Support comments in selectors.
10491074 // $this->assertCount(2, $fooBarBlockComments);
10501075 // $this->assertSame("* Number 4 *", $fooBarBlockComments[0]->getComment());
10511076 // $this->assertSame("* Number 5 *", $fooBarBlockComments[1]->getComment());
10521077
10531078 // Declaration rules.
1079+ self ::assertInstanceOf (RuleSet::class, $ fooBarBlock );
10541080 $ fooBarRules = $ fooBarBlock ->getRules ();
10551081 $ fooBarRule = $ fooBarRules [0 ];
10561082 $ fooBarRuleComments = $ fooBarRule ->getComments ();
10571083 self ::assertCount (1 , $ fooBarRuleComments );
10581084 self ::assertSame (' Number 6 ' , $ fooBarRuleComments [0 ]->getComment ());
10591085
10601086 // Media property.
1087+ self ::assertInstanceOf (Commentable::class, $ nodes [2 ]);
10611088 $ mediaComments = $ nodes [2 ]->getComments ();
10621089 self ::assertCount (0 , $ mediaComments );
10631090
10641091 // Media children.
1092+ self ::assertInstanceOf (CSSList::class, $ nodes [2 ]);
10651093 $ mediaRules = $ nodes [2 ]->getContents ();
1094+ self ::assertInstanceOf (Commentable::class, $ mediaRules [0 ]);
10661095 $ fooBarComments = $ mediaRules [0 ]->getComments ();
10671096 self ::assertCount (1 , $ fooBarComments );
10681097 self ::assertSame ('* Number 10 * ' , $ fooBarComments [0 ]->getComment ());
10691098
10701099 // Media -> declaration -> rule.
1100+ self ::assertInstanceOf (RuleSet::class, $ mediaRules [0 ]);
10711101 $ fooBarRules = $ mediaRules [0 ]->getRules ();
10721102 $ fooBarChildComments = $ fooBarRules [0 ]->getComments ();
10731103 self ::assertCount (1 , $ fooBarChildComments );
@@ -1083,6 +1113,7 @@ public function flatCommentExtractingOneComment(): void
10831113 $ document = $ parser ->parse ();
10841114
10851115 $ contents = $ document ->getContents ();
1116+ self ::assertInstanceOf (RuleSet::class, $ contents [0 ]);
10861117 $ divRules = $ contents [0 ]->getRules ();
10871118 $ comments = $ divRules [0 ]->getComments ();
10881119
@@ -1099,6 +1130,7 @@ public function flatCommentExtractingTwoConjoinedCommentsForOneRule(): void
10991130 $ document = $ parser ->parse ();
11001131
11011132 $ contents = $ document ->getContents ();
1133+ self ::assertInstanceOf (RuleSet::class, $ contents [0 ]);
11021134 $ divRules = $ contents [0 ]->getRules ();
11031135 $ comments = $ divRules [0 ]->getComments ();
11041136
@@ -1116,6 +1148,7 @@ public function flatCommentExtractingTwoSpaceSeparatedCommentsForOneRule(): void
11161148 $ document = $ parser ->parse ();
11171149
11181150 $ contents = $ document ->getContents ();
1151+ self ::assertInstanceOf (RuleSet::class, $ contents [0 ]);
11191152 $ divRules = $ contents [0 ]->getRules ();
11201153 $ comments = $ divRules [0 ]->getComments ();
11211154
@@ -1133,6 +1166,7 @@ public function flatCommentExtractingCommentsForTwoRules(): void
11331166 $ document = $ parser ->parse ();
11341167
11351168 $ contents = $ document ->getContents ();
1169+ self ::assertInstanceOf (RuleSet::class, $ contents [0 ]);
11361170 $ divRules = $ contents [0 ]->getRules ();
11371171 $ rule1Comments = $ divRules [0 ]->getComments ();
11381172 $ rule2Comments = $ divRules [1 ]->getComments ();
@@ -1151,6 +1185,7 @@ public function topLevelCommentExtracting(): void
11511185 $ parser = new Parser ('/*Find Me!*/div {left:10px; text-align:left;} ' );
11521186 $ document = $ parser ->parse ();
11531187 $ contents = $ document ->getContents ();
1188+ self ::assertInstanceOf (Commentable::class, $ contents [0 ]);
11541189 $ comments = $ contents [0 ]->getComments ();
11551190 self ::assertCount (1 , $ comments );
11561191 self ::assertSame ('Find Me! ' , $ comments [0 ]->getComment ());
@@ -1216,6 +1251,7 @@ public function escapedSpecialCaseTokens(): void
12161251 {
12171252 $ document = self ::parsedStructureForFile ('escaped-tokens ' );
12181253 $ contents = $ document ->getContents ();
1254+ self ::assertInstanceOf (RuleSet::class, $ contents [0 ]);
12191255 $ rules = $ contents [0 ]->getRules ();
12201256 $ urlRule = $ rules [0 ];
12211257 $ calcRule = $ rules [1 ];
0 commit comments