Completed
Push — master ( 62fc3d...17d6be )
by Yuri
13:48
created

Bootstrap5DismissibleTemplate   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A wrapMessages() 6 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use Tamtamchik\SimpleFlash\TemplateInterface;
4
use Tamtamchik\SimpleFlash\BaseTemplate;
5
6
if (!session_id()) @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
7
8
require_once '../../vendor/autoload.php';
9
10 View Code Duplication
class Bootstrap5DismissibleTemplate extends BaseTemplate implements TemplateInterface
11
{
12
    protected $prefix  = ''; // every line prefix
13
    protected $postfix = '<br>'; // every postfix
14
    protected $wrapper = '<div class="alert alert-%s alert-dismissible fade show" role="alert">
15
                            %s
16
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
17
                          </div>'; // wrapper over messages of same type
18
19
    /**
20
     * @param $messages - message text
21
     * @param $type     - message type: success, info, warning, error
22
     *
23
     * @return string
24
     */
25
    public function wrapMessages($messages, $type)
26
    {
27
        $type = ($type == 'error') ? 'danger' : $type;
28
29
        return sprintf($this->getWrapper(), $type, $messages);
30
    }
31
}
32
33
flash()->setTemplate(new Bootstrap5DismissibleTemplate())
34
    ->error(['Invalid email!', 'Invalid username!'])
35
    ->warning('Warning message.')
36
    ->info('Info message.')
37
    ->success('Success message!');
38
39
?>
40
41
<!doctype html>
42
<html lang="en">
43
<head>
44
    <meta charset="UTF-8">
45
    <title>Test Bootstrap default template example.</title>
46
    <!-- Latest compiled and minified CSS -->
47
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
48
</head>
49
<body>
50
51
<div class="container" style="width: 600px; margin: 1em auto;">
52
    <?= flash() ?>
53
</div>
54
55
<!-- Latest compiled and minified JavaScript -->
56
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
57
58
</body>
59
</html>
60