@@ -756,171 +756,173 @@ private function getLinesToBeIgnored($filename)
756756 );
757757 }
758758
759- if (!isset ($ this ->ignoredLines [$ filename ])) {
760- $ this ->ignoredLines [$ filename ] = [];
759+ if (isset ($ this ->ignoredLines [$ filename ])) {
760+ return $ this ->ignoredLines [$ filename ];
761+ }
761762
762- if ($ this ->disableIgnoredLines ) {
763- return $ this ->ignoredLines [$ filename ];
764- }
763+ $ this ->ignoredLines [$ filename ] = [];
764+
765+ if ($ this ->disableIgnoredLines ) {
766+ return $ this ->ignoredLines [$ filename ];
767+ }
765768
766- $ ignore = false ;
767- $ stop = false ;
768- $ lines = \file ($ filename );
769- $ numLines = \count ($ lines );
769+ $ lines = \file ($ filename );
770770
771- foreach ($ lines as $ index => $ line ) {
772- if (!\trim ($ line )) {
773- $ this ->ignoredLines [$ filename ][] = $ index + 1 ;
774- }
771+ foreach ($ lines as $ index => $ line ) {
772+ if (!\trim ($ line )) {
773+ $ this ->ignoredLines [$ filename ][] = $ index + 1 ;
775774 }
775+ }
776776
777- if ($ this ->cacheTokens ) {
778- $ tokens = \PHP_Token_Stream_CachingFactory::get ($ filename );
779- } else {
780- $ tokens = new \PHP_Token_Stream ($ filename );
777+ if ($ this ->cacheTokens ) {
778+ $ tokens = \PHP_Token_Stream_CachingFactory::get ($ filename );
779+ } else {
780+ $ tokens = new \PHP_Token_Stream ($ filename );
781+ }
782+
783+ foreach ($ tokens ->getInterfaces () as $ interface ) {
784+ $ interfaceStartLine = $ interface ['startLine ' ];
785+ $ interfaceEndLine = $ interface ['endLine ' ];
786+
787+ foreach (\range ($ interfaceStartLine , $ interfaceEndLine ) as $ line ) {
788+ $ this ->ignoredLines [$ filename ][] = $ line ;
781789 }
790+ }
782791
783- foreach ($ tokens ->getInterfaces () as $ interface ) {
784- $ interfaceStartLine = $ interface ['startLine ' ];
785- $ interfaceEndLine = $ interface ['endLine ' ];
792+ foreach (\array_merge ( $ tokens ->getClasses (), $ tokens -> getTraits ()) as $ classOrTrait ) {
793+ $ classOrTraitStartLine = $ classOrTrait ['startLine ' ];
794+ $ classOrTraitEndLine = $ classOrTrait ['endLine ' ];
786795
787- foreach (\range ($ interfaceStartLine , $ interfaceEndLine ) as $ line ) {
796+ if (empty ($ classOrTrait ['methods ' ])) {
797+ foreach (\range ($ classOrTraitStartLine , $ classOrTraitEndLine ) as $ line ) {
788798 $ this ->ignoredLines [$ filename ][] = $ line ;
789799 }
800+
801+ continue ;
790802 }
791803
792- foreach (\array_merge ($ tokens ->getClasses (), $ tokens ->getTraits ()) as $ classOrTrait ) {
793- $ classOrTraitStartLine = $ classOrTrait ['startLine ' ];
794- $ classOrTraitEndLine = $ classOrTrait ['endLine ' ];
804+ $ firstMethod = \array_shift ($ classOrTrait ['methods ' ]);
805+ $ firstMethodStartLine = $ firstMethod ['startLine ' ];
806+ $ firstMethodEndLine = $ firstMethod ['endLine ' ];
807+ $ lastMethodEndLine = $ firstMethodEndLine ;
795808
796- if (empty ($ classOrTrait ['methods ' ])) {
797- foreach (\range ($ classOrTraitStartLine , $ classOrTraitEndLine ) as $ line ) {
798- $ this ->ignoredLines [$ filename ][] = $ line ;
799- }
809+ do {
810+ $ lastMethod = \array_pop ($ classOrTrait ['methods ' ]);
811+ } while ($ lastMethod !== null && 0 === \strpos ($ lastMethod ['signature ' ], 'anonymousFunction ' ));
800812
801- continue ;
802- }
813+ if ($ lastMethod !== null ) {
814+ $ lastMethodEndLine = $ lastMethod ['endLine ' ];
815+ }
803816
804- $ firstMethod = \array_shift ($ classOrTrait ['methods ' ]);
805- $ firstMethodStartLine = $ firstMethod ['startLine ' ];
806- $ firstMethodEndLine = $ firstMethod ['endLine ' ];
807- $ lastMethodEndLine = $ firstMethodEndLine ;
817+ foreach (\range ($ classOrTraitStartLine , $ firstMethodStartLine ) as $ line ) {
818+ $ this ->ignoredLines [$ filename ][] = $ line ;
819+ }
808820
809- do {
810- $ lastMethod = \array_pop ($ classOrTrait ['methods ' ]);
811- } while ($ lastMethod !== null && 0 === \strpos ($ lastMethod ['signature ' ], 'anonymousFunction ' ));
821+ foreach (\range ($ lastMethodEndLine + 1 , $ classOrTraitEndLine ) as $ line ) {
822+ $ this ->ignoredLines [$ filename ][] = $ line ;
823+ }
824+ }
812825
813- if ($ lastMethod !== null ) {
814- $ lastMethodEndLine = $ lastMethod ['endLine ' ];
815- }
826+ $ ignore = false ;
827+ $ stop = false ;
828+
829+ foreach ($ tokens ->tokens () as $ token ) {
830+ switch (\get_class ($ token )) {
831+ case \PHP_Token_COMMENT::class:
832+ case \PHP_Token_DOC_COMMENT::class:
833+ $ _token = \trim ($ token );
834+ $ _line = \trim ($ lines [$ token ->getLine () - 1 ]);
835+
836+ if ($ _token === '// @codeCoverageIgnore ' ||
837+ $ _token === '//@codeCoverageIgnore ' ) {
838+ $ ignore = true ;
839+ $ stop = true ;
840+ } elseif ($ _token === '// @codeCoverageIgnoreStart ' ||
841+ $ _token === '//@codeCoverageIgnoreStart ' ) {
842+ $ ignore = true ;
843+ } elseif ($ _token === '// @codeCoverageIgnoreEnd ' ||
844+ $ _token === '//@codeCoverageIgnoreEnd ' ) {
845+ $ stop = true ;
846+ }
816847
817- foreach ( \range ( $ classOrTraitStartLine , $ firstMethodStartLine ) as $ line ) {
818- $ this -> ignoredLines [ $ filename ][] = $ line ;
819- }
848+ if (! $ ignore ) {
849+ $ start = $ token -> getLine () ;
850+ $ end = $ start + \substr_count ( $ token , "\n" );
820851
821- foreach (\range ($ lastMethodEndLine + 1 , $ classOrTraitEndLine ) as $ line ) {
822- $ this ->ignoredLines [$ filename ][] = $ line ;
823- }
824- }
852+ // Do not ignore the first line when there is a token
853+ // before the comment
854+ if (0 !== \strpos ($ _token , $ _line )) {
855+ $ start ++;
856+ }
825857
826- foreach ($ tokens ->tokens () as $ token ) {
827- switch (\get_class ($ token )) {
828- case \PHP_Token_COMMENT::class:
829- case \PHP_Token_DOC_COMMENT::class:
830- $ _token = \trim ($ token );
831- $ _line = \trim ($ lines [$ token ->getLine () - 1 ]);
832-
833- if ($ _token === '// @codeCoverageIgnore ' ||
834- $ _token === '//@codeCoverageIgnore ' ) {
835- $ ignore = true ;
836- $ stop = true ;
837- } elseif ($ _token === '// @codeCoverageIgnoreStart ' ||
838- $ _token === '//@codeCoverageIgnoreStart ' ) {
839- $ ignore = true ;
840- } elseif ($ _token === '// @codeCoverageIgnoreEnd ' ||
841- $ _token === '//@codeCoverageIgnoreEnd ' ) {
842- $ stop = true ;
858+ for ($ i = $ start ; $ i < $ end ; $ i ++) {
859+ $ this ->ignoredLines [$ filename ][] = $ i ;
843860 }
844861
845- if (!$ ignore ) {
846- $ start = $ token ->getLine ();
847- $ end = $ start + \substr_count ($ token , "\n" );
848-
849- // Do not ignore the first line when there is a token
850- // before the comment
851- if (0 !== \strpos ($ _token , $ _line )) {
852- $ start ++;
853- }
854-
855- for ($ i = $ start ; $ i < $ end ; $ i ++) {
856- $ this ->ignoredLines [$ filename ][] = $ i ;
857- }
858-
859- // A DOC_COMMENT token or a COMMENT token starting with "/*"
860- // does not contain the final \n character in its text
861- if (isset ($ lines [$ i - 1 ]) && 0 === \strpos ($ _token , '/* ' ) && '*/ ' === \substr (\trim ($ lines [$ i - 1 ]), -2 )) {
862- $ this ->ignoredLines [$ filename ][] = $ i ;
863- }
862+ // A DOC_COMMENT token or a COMMENT token starting with "/*"
863+ // does not contain the final \n character in its text
864+ if (isset ($ lines [$ i - 1 ]) && 0 === \strpos ($ _token , '/* ' ) && '*/ ' === \substr (\trim ($ lines [$ i - 1 ]), -2 )) {
865+ $ this ->ignoredLines [$ filename ][] = $ i ;
864866 }
867+ }
865868
866- break ;
869+ break ;
867870
868- case \PHP_Token_INTERFACE::class:
869- case \PHP_Token_TRAIT::class:
870- case \PHP_Token_CLASS::class:
871- case \PHP_Token_FUNCTION::class:
872- /* @var \PHP_Token_Interface $token */
871+ case \PHP_Token_INTERFACE::class:
872+ case \PHP_Token_TRAIT::class:
873+ case \PHP_Token_CLASS::class:
874+ case \PHP_Token_FUNCTION::class:
875+ /* @var \PHP_Token_Interface $token */
873876
874- $ docblock = $ token ->getDocblock ();
877+ $ docblock = $ token ->getDocblock ();
875878
876- $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
879+ $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
877880
878- if (\strpos ($ docblock , '@codeCoverageIgnore ' ) || ($ this ->ignoreDeprecatedCode && \strpos ($ docblock , '@deprecated ' ))) {
879- $ endLine = $ token ->getEndLine ();
881+ if (\strpos ($ docblock , '@codeCoverageIgnore ' ) || ($ this ->ignoreDeprecatedCode && \strpos ($ docblock , '@deprecated ' ))) {
882+ $ endLine = $ token ->getEndLine ();
880883
881- for ($ i = $ token ->getLine (); $ i <= $ endLine ; $ i ++) {
882- $ this ->ignoredLines [$ filename ][] = $ i ;
883- }
884+ for ($ i = $ token ->getLine (); $ i <= $ endLine ; $ i ++) {
885+ $ this ->ignoredLines [$ filename ][] = $ i ;
884886 }
887+ }
885888
886- break ;
889+ break ;
887890
888- case \PHP_Token_ENUM::class:
889- $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
891+ case \PHP_Token_ENUM::class:
892+ $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
890893
891- break ;
894+ break ;
892895
893- case \PHP_Token_NAMESPACE::class:
894- $ this ->ignoredLines [$ filename ][] = $ token ->getEndLine ();
896+ case \PHP_Token_NAMESPACE::class:
897+ $ this ->ignoredLines [$ filename ][] = $ token ->getEndLine ();
895898
896- // Intentional fallthrough
897- case \PHP_Token_DECLARE::class:
898- case \PHP_Token_OPEN_TAG::class:
899- case \PHP_Token_CLOSE_TAG::class:
900- case \PHP_Token_USE::class:
901- $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
899+ // Intentional fallthrough
900+ case \PHP_Token_DECLARE::class:
901+ case \PHP_Token_OPEN_TAG::class:
902+ case \PHP_Token_CLOSE_TAG::class:
903+ case \PHP_Token_USE::class:
904+ $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
902905
903- break ;
904- }
906+ break ;
907+ }
905908
906- if ($ ignore ) {
907- $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
909+ if ($ ignore ) {
910+ $ this ->ignoredLines [$ filename ][] = $ token ->getLine ();
908911
909- if ($ stop ) {
910- $ ignore = false ;
911- $ stop = false ;
912- }
912+ if ($ stop ) {
913+ $ ignore = false ;
914+ $ stop = false ;
913915 }
914916 }
917+ }
915918
916- $ this ->ignoredLines [$ filename ][] = $ numLines + 1 ;
919+ $ this ->ignoredLines [$ filename ][] = \count ( $ lines ) + 1 ;
917920
918- $ this ->ignoredLines [$ filename ] = \array_unique (
919- $ this ->ignoredLines [$ filename ]
920- );
921+ $ this ->ignoredLines [$ filename ] = \array_unique (
922+ $ this ->ignoredLines [$ filename ]
923+ );
921924
922- \sort ($ this ->ignoredLines [$ filename ]);
923- }
925+ \sort ($ this ->ignoredLines [$ filename ]);
924926
925927 return $ this ->ignoredLines [$ filename ];
926928 }
0 commit comments