• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 5046064611

pending completion
5046064611

push

github

GitHub
perf: native_function_invocation (#1697)

322 of 322 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.17
/src/Logger/PrintLogger.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Logger;
15

16
use Cecil\Builder;
17
use Psr\Log\AbstractLogger;
18
use Psr\Log\InvalidArgumentException;
19
use Psr\Log\LogLevel;
20

21
class PrintLogger extends AbstractLogger
22
{
23
    /** @var int */
24
    protected $printLevelMax = null;
25

26
    /** @var array */
27
    protected $verbosityLevelMap = [
28
        LogLevel::EMERGENCY => Builder::VERBOSITY_NORMAL,
29
        LogLevel::ALERT     => Builder::VERBOSITY_NORMAL,
30
        LogLevel::CRITICAL  => Builder::VERBOSITY_NORMAL,
31
        LogLevel::ERROR     => Builder::VERBOSITY_NORMAL,
32
        LogLevel::WARNING   => Builder::VERBOSITY_NORMAL,
33
        LogLevel::NOTICE    => Builder::VERBOSITY_NORMAL,
34
        LogLevel::INFO      => Builder::VERBOSITY_VERBOSE,
35
        LogLevel::DEBUG     => Builder::VERBOSITY_DEBUG,
36
    ];
37

38
    /**
39
     * Print only the $printLevelMax.
40
     */
41
    public function __construct(int $printLevelMax = null)
42
    {
43
        $this->printLevelMax = $printLevelMax;
1✔
44
    }
45

46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @return void
50
     */
51
    public function log($level, $message, array $context = [])
52
    {
53
        if (!isset($this->verbosityLevelMap[$level])) {
1✔
54
            throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level));
×
55
        }
56

57
        if ($this->printLevelMax !== null && $this->verbosityLevelMap[$level] > $this->printLevelMax) {
1✔
58
            return;
×
59
        }
60

61
        $level = $level != LogLevel::INFO ? "[$level] " : '';
1✔
62

63
        if (isset($context['progress'])) {
1✔
64
            printf(
1✔
65
                "%s%s (%s/%s)\n",
1✔
66
                $level,
1✔
67
                $this->interpolate($message, $context),
1✔
68
                $context['progress'][0],
1✔
69
                $context['progress'][1]
1✔
70
            );
1✔
71

72
            return;
1✔
73
        }
74

75
        printf("%s%s\n", $level, $this->interpolate($message, $context));
1✔
76
    }
77

78
    /**
79
     * Interpolates context values into the message placeholders.
80
     *
81
     * @author PHP Framework Interoperability Group
82
     */
83
    protected function interpolate(string $message, array $context): string
84
    {
85
        if (false === strpos($message, '{')) {
1✔
86
            return $message;
1✔
87
        }
88

89
        $replacements = [];
×
90
        foreach ($context as $key => $val) {
×
91
            if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
×
92
                $replacements["{{$key}}"] = $val;
×
93
            } elseif ($val instanceof \DateTimeInterface) {
×
94
                $replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
×
95
            } elseif (\is_object($val)) {
×
96
                $replacements["{{$key}}"] = '[object ' . \get_class($val) . ']';
×
97
            } else {
98
                $replacements["{{$key}}"] = '[' . \gettype($val) . ']';
×
99
            }
100
        }
101

102
        return strtr($message, $replacements);
×
103
    }
104

105
    /**
106
     * Format expression to string.
107
     */
108
    public static function format($expression): string
109
    {
110
        return str_replace(["\n", ' '], '', var_export($expression, true));
×
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc