Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1430027
StringUtil.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
StringUtil.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
Phan\Library
;
use
Phan\AST\ASTReverter
;
/**
* StringUtil contains methods to simplify working with strings in Phan and its plugins.
*/
class
StringUtil
{
/**
* Encode a scalar value in a compact, unambiguous representation for emitted issues.
* The encoder used by encodeValue may change.
* This aims to fit on a single line.
*
* @param string|int|float|bool|null $value
*/
public
static
function
encodeValue
(
$value
):
string
{
if
(
\is_string
(
$value
)
&&
\preg_match
(
'/([
\0
-
\1
5
\1
6-
\3
7])/'
,
$value
))
{
// Use double quoted strings if this contains newlines, tabs, control characters, etc.
return
'"'
.
ASTReverter
::
escapeInnerString
(
$value
,
'"'
)
.
'"'
;
}
return
self
::
varExportPretty
(
$value
);
}
/**
* Encode a scalar value in a compact, unambiguous representation for emitted issues.
* The encoder used by encodeValue may change.
* This fits on a single line.
*
* @param ?(int|bool|string|array|float) $value
*/
public
static
function
varExportPretty
(
$value
):
string
{
return
\var_representation
(
$value
,
\VAR_REPRESENTATION_SINGLE_LINE
);
}
/**
* JSON encodes a value - Guaranteed to return a string.
* @param string|int|float|bool|null|array|object $value
*/
public
static
function
jsonEncode
(
$value
):
string
{
$result
=
\json_encode
(
$value
,
\JSON_UNESCAPED_SLASHES
|
\JSON_UNESCAPED_UNICODE
|
\JSON_PARTIAL_OUTPUT_ON_ERROR
);
return
\is_string
(
$result
)
?
$result
:
'(invalid data)'
;
}
/**
* Encode a list of values in a compact, unambiguous representation for emitted issues.
* @param list<string|int|float|bool> $values
*/
public
static
function
encodeValueList
(
string
$separator
,
array
$values
):
string
{
return
\implode
(
$separator
,
\array_map
([
self
::
class
,
'encodeValue'
],
$values
)
);
}
/**
* Coerce $str to valid utf-8
*/
public
static
function
asUtf8
(
string
$str
):
string
{
return
\mb_convert_encoding
(
$str
,
'UTF-8'
,
'UTF-8'
)
?:
$str
;
}
/**
* Coerce $str to valid utf-8 and replace newlines with placeholders
*/
public
static
function
asSingleLineUtf8
(
string
$str
):
string
{
if
(!
\preg_match
(
"@[
\\
n
\\
r
\x
80-
\x
ff]@"
,
$str
))
{
// Around 5x faster for the common case of being ASCII without newlines.
return
$str
;
}
return
\str_replace
([
"
\n
"
,
"
\r
"
],
"�"
,
self
::
asUtf8
(
$str
));
}
/**
* Returns true if the provided argument is a non-zero length string.
* Unlike (bool)$str, this allows the literal string '0', and rejects types other than strings.
*
* @param ?string|?false $str typical inputs are nullable/falsable strings
* @phan-assert string $str TODO: This unconditionally sets the type of $str to string - add an equivalent that only sets the type when true.
* @psalm-assert-if-true string $str
*/
public
static
function
isNonZeroLengthString
(
$str
):
bool
{
return
\is_string
(
$str
)
&&
$str
!==
''
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 18:18 (7 h, 13 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
be/78/3e471895dfab391b8e9d3a8628a7
Default Alt Text
StringUtil.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment