Prints class constants

@param ReflectionConstant[] $constants List of constant instance
@param string               $format    (optional) Constant formatter
@param int                  $indent    (optional) Indent multiplier

@return string
324247
protected function writeConstantElements($constants, $format = '%s %s\l', $indent = -1)
    {
        $constantString = '';

        foreach ($constants as $name => $value) {
            $line = sprintf($format, '+', $name);
            if ($indent >= 0) {
                $constantString .= $this->formatLine($line, $indent);
            } else {
                $constantString .= $line;
            }
        }
        return $constantString;
    }
751382
public function getConstant($constantName)
    {
        if (!\is_string($constantName)) {
            throw new InvalidArgumentException('$constantName must be string type');
        }

        if (empty($constantName)) {
            throw new InvalidArgumentException('$constantName must not be empty: '.$constantName);
        }

        if (isset($this->constants[$constantName])) {
            return $this->constants[$constantName];
        }

        $extendsClassDefinition = $this->getExtendsClassDefinition();
        if ($extendsClassDefinition) {
            if ($extendsClassDefinition->hasConstant($constantName)) {
                return $extendsClassDefinition->getConstant($constantName);
            }
        }

        /*
         * Gets constant from interfaces
         */
        return $this->getConstantFromInterfaces($constantName);
    }
1327256
public static function getConstants()
    {
        if (empty(self::$_constantsCache)) {
            $reflectionClass = new \ReflectionClass(get_called_class());
            self::$_constantsCache = $reflectionClass->getConstants();
        }
        return self::$_constantsCache;
    }
366051
protected function writePropertyElements($properties, $format = '%s %s\l', $indent = -1)
    {
        $propertyString = '';

        foreach ($properties as $property) {
            if ($property->isPrivate()) {
                $visibility = '-';
            } elseif ($property->isProtected()) {
                $visibility = '#';
            } else {
                $visibility = '+';
            }

            $line = sprintf(
                $format,
                $visibility,
                $property->getName()
            );
            if ($indent >= 0) {
                $propertyString .= $this->formatLine($line, $indent);
            } else {
                $propertyString .= $line;
            }
        }
        return $propertyString;
    }
1356869
protected function writeMethodElements($methods, $format = '%s %s()\l', $indent = -1)
    {
        $methodString = '';

        foreach ($methods as $method) {
            if ($method->isPrivate()) {
                $visibility = '-';
            } elseif ($method->isProtected()) {
                $visibility = '#';
            } else {
                $visibility = '+';
            }

            if ($method->isStatic()) {
                $modifier = '<u>%s</u>';
            } elseif ($method->isAbstract()) {
                $modifier = '<i>%s</i>';
            } else {
                $modifier = '%s';
            }

            $line = sprintf(
                $format,
                $visibility,
                sprintf($modifier, $method->getShortName())
            );
            if ($indent >= 0) {
                $methodString .= $this->formatLine($line, $indent);
            } else {
                $methodString .= $line;
            }
        }
        return $methodString;
    }
179250
public function mprint($A, $format='%01.2f', $width=2)
    {
        $spacing = '';
        $m = count($A);
        $n = count($A[0]);
        for ($i = 0; $i < $width; $i++) {
            $spacing .= '&nbsp;';
        }
        for ($i = 0; $i < $m; $i++) {
            for ($j = 0; $j < $n; $j++) {
                $formatted = sprintf($format, $A[$i][$j]);
                echo $formatted . $spacing;
            }
            echo '<br />';
        }
    }
335893
private function buildFields(array $fields, $indent, array &$result)
    {
        $stringified_field_names = [];
        $fields_with_default_value = [];

        foreach ($fields as $field) {
            if ($field instanceof ScalarField && $field->getShouldBeAddedToModel()) {
                $stringified_field_names[] = var_export($field->getName(), true);

                if ($field->getName() != 'id' && $field->getDefaultValue() !== null) {
                    $fields_with_default_value[$field->getName()] = $field->getDefaultValue();
                }
            }
        }

        $result[] = '';
        $result[] = $indent . '/**';
        $result[] = $indent . ' * Table fields that are managed by this entity.';
        $result[] = $indent . ' *';
        $result[] = $indent . ' * @var array';
        $result[] = $indent . ' */';
        $result[] = $indent . 'protected $fields = [' . implode(', ', $stringified_field_names) . '];';

        if (count($fields_with_default_value)) {
            $result[] = '';
            $result[] = $indent . '/**';
            $result[] = $indent . ' * List of default field values.';
            $result[] = $indent . ' *';
            $result[] = $indent . ' * @var array';
            $result[] = $indent . ' */';
            $result[] = $indent . 'protected $default_field_values = [';

            foreach ($fields_with_default_value as $field_name => $default_value) {
                $result[] = $indent . '   ' . var_export($field_name, true) . ' => ' . var_export($default_value, true) . ',';
            }

            $result[] = $indent . '];';
        }
    }
1362306
private function ipAddressCalculation($format, $separator = '')
    {
        return implode($separator, array_map(
            function ($quad) use ($format) {
                return sprintf($format, $quad);
            },
            $this->quads
        ));
    }
1199076
private function writeConstantClass(): void
  {
    // Get the class loader.
    /** @var \Composer\Autoload\ClassLoader $loader */
    $loader = spl_autoload_functions()[0][0];

    // Find the source file of the constant class.
    $file_name = $loader->findFile($this->className);
    if ($file_name===false)
    {
      throw new RuntimeException("ClassLoader can not find class '%s'.", $this->className);
    }

    // Read the source of the class without actually loading the class. Otherwise, we can not (re)load the class in
    // \SetBased\Stratum\MySql\RoutineLoaderCommand::getConstants.
    $source = file_get_contents($file_name);
    if ($source===false)
    {
      throw new RuntimeException("Unable the open source file '%s'.", $file_name);
    }
    $source_lines = explode("\n", $source);

    // Search for the lines where to insert and replace constant declaration statements.
    $line_numbers = $this->extractLines($source);
    if (!isset($line_numbers[0]))
    {
      throw new RuntimeException("Annotation not found in '%s'.", $file_name);
    }

    // Generate the constant declaration statements.
    $constants = $this->makeConstantStatements();

    // Insert new and replace old (if any) constant declaration statements.
    $tmp1         = array_splice($source_lines, 0, $line_numbers[1]);
    $tmp2         = array_splice($source_lines, (isset($line_numbers[2])) ? $line_numbers[2] - $line_numbers[1] : 0);
    $source_lines = array_merge($tmp1, $constants, $tmp2);

    // Save the configuration file.
    $this->writeTwoPhases($file_name, implode("\n", $source_lines));
  }
650586
public static function indent(string $s, int $level = 1, string $chars = "\t"): string
    {
        if ($level > 0) {
            $s = static::replace($s, '#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . \str_repeat($chars, $level));
        }

        return $s;
    }
1119772
private function collectSelfConstants()
    {
        $expressionSolver = new NodeExpressionResolver($this);
        $localConstants   = [];

        // constants can be only top-level nodes in the class, so we can scan them directly
        foreach ($this->classLikeNode->stmts as $classLevelNode) {
            if ($classLevelNode instanceof ClassConst) {
                $nodeConstants = $classLevelNode->consts;
                if (!empty($nodeConstants)) {
                    foreach ($nodeConstants as $nodeConstant) {
                        $expressionSolver->process($nodeConstant->value);
                        $localConstants[$nodeConstant->name->toString()] = $expressionSolver->getValue();
                        $this->constants = $localConstants + $this->constants;
                    }
                }
            }
        }
    }
387417
public static function formatTrace(array $trace, $indent = '') {
        $appRoot = self::di('config')['app.dir.root'];
        $result  = '';

        $size = sizeof($trace);
        $callLen = $lineLen = 0;

        for ($i=0; $i < $size; $i++) {               // align FILE and LINE
            $frame = &$trace[$i];

            $call = self::getFQFunctionName($frame, $nsLowerCase=true);

            if ($call!='{main}' && !strEndsWith($call, '{closure}'))
                $call.='()';
            $callLen = max($callLen, strlen($call));
            $frame['call'] = $call;

            $frame['line'] = isset($frame['line']) ? ' # line '.$frame['line'].',' : '';
            $lineLen = max($lineLen, strlen($frame['line']));

            if (isset($frame['file']))                 $frame['file'] = ' file: '.(!$appRoot ? $frame['file'] : strRightFrom($frame['file'], $appRoot.DIRECTORY_SEPARATOR, 1, false, $frame['file']));
            elseif (strStartsWith($call, 'phalcon\\')) $frame['file'] = ' [php-phalcon]';
            else                                       $frame['file'] = ' [php]';
        }
        if ($appRoot) {
            $trace[] = ['call'=>'', 'line'=>'', 'file'=>' file base: '.$appRoot];
            $i++;
        }

        for ($i=0; $i < $size; $i++) {
            $result .= $indent.str_pad($trace[$i]['call'], $callLen).' '.str_pad($trace[$i]['line'], $lineLen).$trace[$i]['file'].NL;
        }

        return $result;
    }
238401
final private static function guardConstants(array $constants): void
    {
        $duplicates = [];
        foreach ($constants as $value) {
            $names = array_keys($constants, $value, $strict = true);
            if (count($names) > 1) {
                $duplicates[VarPrinter::toString($value)] = $names;
            }
        }
        if (!empty($duplicates)) {
            $list = array_map(function ($names) use ($constants) {
                return sprintf('(%s)=%s', implode('|', $names), VarPrinter::toString($constants[$names[0]]));
            }, $duplicates);
            $message = sprintf('Duplicate enum values: %s', implode(', ', $list));
            throw new DomainException($message);
        }
    }
1333311
private static function formatConstant($reflector)
    {
        $value = $reflector->getValue();
        $style = self::getTypeStyle($value);

        return \sprintf(
            '<keyword>define</keyword>(<string>%s</string>, <%s>%s</%s>)',
            OutputFormatter::escape(Json::encode($reflector->getName())),
            $style,
            OutputFormatter::escape(Json::encode($value)),
            $style
        );
    }
99023
protected function prepareConstants(array $constants)
    {
        // My kingdom for a generator.
        $ret = [];

        foreach ($constants as $name => $constant) {
            if ($this->showItem($name)) {
                $ret[$name] = [
                    'name'  => $name,
                    'style' => self::IS_CONSTANT,
                    'value' => $this->presentRef($constant->getValue()),
                ];
            }
        }

        return $ret;
    }
453211
private function getPresentationExtensionConstant()
    {
        $retour = [];
        if (empty($this->const_pptsExt)) {
            $constants = $this->getConstants();
            foreach ($constants as $key => $constant) {
                $pos = strpos($key, 'EXT_PPTS');
                if ($pos !== false) {
                    $retour[$key] = $constant;
                }
            }
            $this->const_pptsExt = $retour;
        } else {
            $retour = $this->const_pptsExt;
        }
        return $retour;
    }
462831
public function format($format = null)
    {
        if (!$format) {
            $format = $this->getFormat();
        }

        return parent::format($format ?: static::$toStringFormat);
    }