Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5965128
LineCountingVisitor.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
LineCountingVisitor.php
View Options
<?php
declare
(
strict_types
=
1
);
/*
* This file is part of sebastian/lines-of-code.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
SebastianBergmann\LinesOfCode
;
use
function
array_merge
;
use
function
array_unique
;
use
function
count
;
use
PhpParser\Comment
;
use
PhpParser\Node
;
use
PhpParser\Node\Expr
;
use
PhpParser\NodeVisitorAbstract
;
final
class
LineCountingVisitor
extends
NodeVisitorAbstract
{
/**
* @var int
*/
private
$linesOfCode
;
/**
* @var Comment[]
*/
private
$comments
=
[];
/**
* @var int[]
*/
private
$linesWithStatements
=
[];
public
function
__construct
(
int
$linesOfCode
)
{
$this
->
linesOfCode
=
$linesOfCode
;
}
public
function
enterNode
(
Node
$node
):
void
{
$this
->
comments
=
array_merge
(
$this
->
comments
,
$node
->
getComments
());
if
(!
$node
instanceof
Expr
)
{
return
;
}
$this
->
linesWithStatements
[]
=
$node
->
getStartLine
();
}
public
function
result
():
LinesOfCode
{
$commentLinesOfCode
=
0
;
foreach
(
$this
->
comments
()
as
$comment
)
{
$commentLinesOfCode
+=
(
$comment
->
getEndLine
()
-
$comment
->
getStartLine
()
+
1
);
}
return
new
LinesOfCode
(
$this
->
linesOfCode
,
$commentLinesOfCode
,
$this
->
linesOfCode
-
$commentLinesOfCode
,
count
(
array_unique
(
$this
->
linesWithStatements
))
);
}
/**
* @return Comment[]
*/
private
function
comments
():
array
{
$comments
=
[];
foreach
(
$this
->
comments
as
$comment
)
{
$comments
[
$comment
->
getStartLine
()
.
'_'
.
$comment
->
getStartTokenPos
()
.
'_'
.
$comment
->
getEndLine
()
.
'_'
.
$comment
->
getEndTokenPos
()]
=
$comment
;
}
return
$comments
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 10:46 (1 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
05/ca/725f329777619e51bfc77a13f9e8
Default Alt Text
LineCountingVisitor.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment