Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5957805
ValidFunctionNamePass.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
ValidFunctionNamePass.php
View Options
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2023 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Psy\CodeCleaner
;
use
PhpParser\Node
;
use
PhpParser\Node\Stmt\Do_
;
use
PhpParser\Node\Stmt\Function_
;
use
PhpParser\Node\Stmt\If_
;
use
PhpParser\Node\Stmt\Switch_
;
use
PhpParser\Node\Stmt\While_
;
use
Psy\Exception\FatalErrorException
;
/**
* Validate that function calls will succeed.
*
* This pass throws a FatalErrorException rather than letting PHP run
* headfirst into a real fatal error and die.
*/
class
ValidFunctionNamePass
extends
NamespaceAwarePass
{
private
int
$conditionalScopes
=
0
;
/**
* Store newly defined function names on the way in, to allow recursion.
*
* @throws FatalErrorException if a function is redefined in a non-conditional scope
*
* @param Node $node
*
* @return int|Node|null Replacement node (or special return value)
*/
public
function
enterNode
(
Node
$node
)
{
parent
::
enterNode
(
$node
);
if
(
self
::
isConditional
(
$node
))
{
$this
->
conditionalScopes
++;
}
elseif
(
$node
instanceof
Function_
)
{
$name
=
$this
->
getFullyQualifiedName
(
$node
->
name
);
// @todo add an "else" here which adds a runtime check for instances where we can't tell
// whether a function is being redefined by static analysis alone.
if
(
$this
->
conditionalScopes
===
0
)
{
if
(
\function_exists
(
$name
)
||
isset
(
$this
->
currentScope
[
\strtolower
(
$name
)]))
{
$msg
=
\sprintf
(
'Cannot redeclare %s()'
,
$name
);
throw
new
FatalErrorException
(
$msg
,
0
,
\E_ERROR
,
null
,
$node
->
getStartLine
());
}
}
$this
->
currentScope
[
\strtolower
(
$name
)]
=
true
;
}
}
/**
* @param Node $node
*
* @return int|Node|Node[]|null Replacement node (or special return value)
*/
public
function
leaveNode
(
Node
$node
)
{
if
(
self
::
isConditional
(
$node
))
{
$this
->
conditionalScopes
--;
}
}
private
static
function
isConditional
(
Node
$node
)
{
return
$node
instanceof
If_
||
$node
instanceof
While_
||
$node
instanceof
Do_
||
$node
instanceof
Switch_
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 05:21 (2 w, 6 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
7e/2c/d4c4cfc75d655c972387ca465c29
Default Alt Text
ValidFunctionNamePass.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment