Skip to content

Commit

Permalink
Utils: Reorder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Oct 27, 2024
1 parent 716e87c commit 41ccd89
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 124 deletions.
Binary file modified build/kint.phar
Binary file not shown.
248 changes: 124 additions & 124 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,74 +169,6 @@ public static function isAssoc(array $array): bool
return (bool) \count(\array_filter(\array_keys($array), 'is_string'));
}

public static function composerGetExtras(string $key = 'kint'): array
{
if (0 === \strpos(KINT_DIR, 'phar://')) {
// Only run inside phar file, so skip for code coverage
return []; // @codeCoverageIgnore
}

$extras = [];

$folder = KINT_DIR.'/vendor';

for ($i = 0; $i < 4; ++$i) {
$installed = $folder.'/composer/installed.json';

if (\file_exists($installed) && \is_readable($installed)) {
$packages = \json_decode(\file_get_contents($installed), true);

if (!\is_array($packages)) {
continue;
}

// Composer 2.0 Compatibility: packages are now wrapped into a "packages" top level key instead of the whole file being the package array
// @see https://getcomposer.org/upgrade/UPGRADE-2.0.md
foreach ($packages['packages'] ?? $packages as $package) {
if (\is_array($package['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $package['extra'][$key]);
}
}

$folder = \dirname($folder);

if (\file_exists($folder.'/composer.json') && \is_readable($folder.'/composer.json')) {
$composer = \json_decode(\file_get_contents($folder.'/composer.json'), true);

if (\is_array($composer['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $composer['extra'][$key]);
}
}

break;
}

$folder = \dirname($folder);
}

return $extras;
}

/**
* @codeCoverageIgnore
*/
public static function composerSkipFlags(): void
{
if (\defined('KINT_SKIP_FACADE') && \defined('KINT_SKIP_HELPERS')) {
return;
}

$extras = self::composerGetExtras();

if (!empty($extras['disable-facade']) && !\defined('KINT_SKIP_FACADE')) {
\define('KINT_SKIP_FACADE', true);
}

if (!empty($extras['disable-helpers']) && !\defined('KINT_SKIP_HELPERS')) {
\define('KINT_SKIP_HELPERS', true);
}
}

/**
* @psalm-assert-if-true list<TraceFrame> $trace
*/
Expand Down Expand Up @@ -291,33 +223,6 @@ public static function traceFrameIsListed(array $frame, array $matches): bool
return \in_array($called, $matches, true);
}

/** @psalm-pure */
public static function isValidPhpName(string $name): bool
{
return (bool) \preg_match('/^[a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*$/', $name);
}

/** @psalm-pure */
public static function isValidPhpNamespace(string $ns): bool
{
$parts = \explode('\\', $ns);
if ('' === \reset($parts)) {
\array_shift($parts);
}

if (!\count($parts)) {
return false;
}

foreach ($parts as $part) {
if (!self::isValidPhpName($part)) {
return false;
}
}

return true;
}

/** @psalm-pure */
public static function normalizeAliases(array $aliases): array
{
Expand Down Expand Up @@ -350,6 +255,33 @@ public static function normalizeAliases(array $aliases): array
return \array_values($aliases);
}

/** @psalm-pure */
public static function isValidPhpName(string $name): bool
{
return (bool) \preg_match('/^[a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*$/', $name);
}

/** @psalm-pure */
public static function isValidPhpNamespace(string $ns): bool
{
$parts = \explode('\\', $ns);
if ('' === \reset($parts)) {
\array_shift($parts);
}

if (!\count($parts)) {
return false;
}

foreach ($parts as $part) {
if (!self::isValidPhpName($part)) {
return false;
}
}

return true;
}

/**
* trigger_error before PHP 8.1 truncates the error message at nul
* so we have to sanitize variable strings before using them.
Expand Down Expand Up @@ -407,6 +339,45 @@ public static function truncateString(string $input, int $length = PHP_INT_MAX,
return $input;
}

/**
* @psalm-return Encoding
*/
public static function detectEncoding(string $string)
{
if (\function_exists('mb_detect_encoding')) {
$ret = \mb_detect_encoding($string, self::$char_encodings, true);
if (false !== $ret) {
return $ret;
}
}

// Pretty much every character encoding uses first 32 bytes as control
// characters. If it's not a multi-byte format it's safe to say matching
// any control character besides tab, nl, and cr means it's binary.
if (\preg_match('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/', $string)) {
return false;
}

if (\function_exists('iconv')) {
foreach (self::$legacy_encodings as $encoding) {
// Iconv detection works by triggering
// "Detected an illegal character in input string" notices
// This notice does not become a TypeError with strict_types
// so we don't have to wrap this in a try catch
if (@\iconv($encoding, $encoding, $string) === $string) {
return $encoding;
}
}
} elseif (!\function_exists('mb_detect_encoding')) { // @codeCoverageIgnore
// If a user has neither mb_detect_encoding, nor iconv, nor the
// polyfills, there's not much we can do about it...
// Pretend it's ASCII and pray the browser renders it properly.
return 'ASCII'; // @codeCoverageIgnore
}

return false;
}

/**
* @psalm-param Encoding $encoding
*/
Expand Down Expand Up @@ -448,42 +419,71 @@ public static function substr(string $string, int $start, ?int $length = null, $
return \substr($string, $start, $length ?? PHP_INT_MAX);
}

/**
* @psalm-return Encoding
*/
public static function detectEncoding(string $string)
public static function composerGetExtras(string $key = 'kint'): array
{
if (\function_exists('mb_detect_encoding')) {
$ret = \mb_detect_encoding($string, self::$char_encodings, true);
if (false !== $ret) {
return $ret;
}
if (0 === \strpos(KINT_DIR, 'phar://')) {
// Only run inside phar file, so skip for code coverage
return []; // @codeCoverageIgnore
}

// Pretty much every character encoding uses first 32 bytes as control
// characters. If it's not a multi-byte format it's safe to say matching
// any control character besides tab, nl, and cr means it's binary.
if (\preg_match('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/', $string)) {
return false;
}
$extras = [];

if (\function_exists('iconv')) {
foreach (self::$legacy_encodings as $encoding) {
// Iconv detection works by triggering
// "Detected an illegal character in input string" notices
// This notice does not become a TypeError with strict_types
// so we don't have to wrap this in a try catch
if (@\iconv($encoding, $encoding, $string) === $string) {
return $encoding;
$folder = KINT_DIR.'/vendor';

for ($i = 0; $i < 4; ++$i) {
$installed = $folder.'/composer/installed.json';

if (\file_exists($installed) && \is_readable($installed)) {
$packages = \json_decode(\file_get_contents($installed), true);

if (!\is_array($packages)) {
continue;
}

// Composer 2.0 Compatibility: packages are now wrapped into a "packages" top level key instead of the whole file being the package array
// @see https://getcomposer.org/upgrade/UPGRADE-2.0.md
foreach ($packages['packages'] ?? $packages as $package) {
if (\is_array($package['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $package['extra'][$key]);
}
}

$folder = \dirname($folder);

if (\file_exists($folder.'/composer.json') && \is_readable($folder.'/composer.json')) {
$composer = \json_decode(\file_get_contents($folder.'/composer.json'), true);

if (\is_array($composer['extra'][$key] ?? null)) {
$extras = \array_replace($extras, $composer['extra'][$key]);
}
}

break;
}
} elseif (!\function_exists('mb_detect_encoding')) { // @codeCoverageIgnore
// If a user has neither mb_detect_encoding, nor iconv, nor the
// polyfills, there's not much we can do about it...
// Pretend it's ASCII and pray the browser renders it properly.
return 'ASCII'; // @codeCoverageIgnore

$folder = \dirname($folder);
}

return false;
return $extras;
}

/**
* @codeCoverageIgnore
*/
public static function composerSkipFlags(): void
{
if (\defined('KINT_SKIP_FACADE') && \defined('KINT_SKIP_HELPERS')) {
return;
}

$extras = self::composerGetExtras();

if (!empty($extras['disable-facade']) && !\defined('KINT_SKIP_FACADE')) {
\define('KINT_SKIP_FACADE', true);
}

if (!empty($extras['disable-helpers']) && !\defined('KINT_SKIP_HELPERS')) {
\define('KINT_SKIP_HELPERS', true);
}
}
}

0 comments on commit 41ccd89

Please sign in to comment.