Passed
Branch master (7e303a)
by Michael
02:15
created

Seo::getTitle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 64
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 43
nc 6
nop 2
dl 0
loc 64
rs 9.232
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 * $Id
7
 * Module: Publisher
8
 * Author: Sudhaker Raj <http://xoops.biz>
9
 * Licence: GNU
10
 */
11
12
// define PUBLISHER_SEO_ENABLED in mainfile.php, possible values
13
//   are "rewrite" & "path-info"
14
15
/**
16
 * Create a title for the shortUrl field of an article
17
 *
18
 * @credit psylove
19
 *
20
 * @var    string $title   title of the article
21
 * @var    string $withExt do we add an html extension or not
22
 * @return string sort_url for the article
23
 */
24
25
use XoopsModules\Publisher;
26
27
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
28
29
require_once dirname(__DIR__) . '/include/common.php';
30
31
/**
32
 * Class Seo
33
 */
34
class Seo
35
{
36
    /**
37
     * @param string $title
38
     * @param bool   $withExt
39
     *
40
     * @return mixed|string
41
     */
42
    public static function getTitle($title = '', $withExt = true)
43
    {
44
45
        /**
46
         * if XOOPS ML is present, let's sanitize the title with the current language
47
         */
48
        $myts = \MyTextSanitizer::getInstance();
49
        if (method_exists($myts, 'formatForML')) {
50
            $title = $myts->formatForML($title);
51
        }
52
53
        // Transformation de la chaine en minuscule
54
        // Codage de la chaine afin d'éviter les erreurs 500 en cas de caractères imprévus
55
        $title = rawurlencode(mb_strtolower($title));
56
57
        // Transformation des ponctuations
58
        $pattern    = [
59
            '/%09/', // Tab
60
            '/%20/', // Space
61
            '/%21/', // !
62
            '/%22/', // "
63
            '/%23/', // #
64
            '/%25/', // %
65
            '/%26/', // &
66
            '/%27/', // '
67
            '/%28/', // (
68
            '/%29/', // )
69
            '/%2C/', // ,
70
            '/%2F/', // /
71
            '/%3A/', // :
72
            '/%3B/', // ;
73
            '/%3C/', // <
74
            '/%3D/', // =
75
            '/%3E/', // >
76
            '/%3F/', // ?
77
            '/%40/', // @
78
            '/%5B/', // [
79
            '/%5C/', // \
80
            '/%5D/', // ]
81
            '/%5E/', // ^
82
            '/%7B/', // {
83
            '/%7C/', // |
84
            '/%7D/', // }
85
            '/%7E/', // ~
86
            "/\./", // .
87
        ];
88
        $repPattern = ['-', '-', '', '', '', '-100', '', '-', '', '', '', '-', '', '', '', '-', '', '', '-at-', '', '-', '', '-', '', '-', '', '-', ''];
89
        $title      = preg_replace($pattern, $repPattern, $title);
90
91
        // Transformation des caractères accentués
92
        //                  è        é        ê        ë        ç        à        â        ä        î        ï        ù        ü        û        ô        ö
93
        $pattern    = ['/%B0/', '/%E8/', '/%E9/', '/%EA/', '/%EB/', '/%E7/', '/%E0/', '/%E2/', '/%E4/', '/%EE/', '/%EF/', '/%F9/', '/%FC/', '/%FB/', '/%F4/', '/%F6/'];
94
        $repPattern = ['-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o'];
95
        $title      = preg_replace($pattern, $repPattern, $title);
96
97
        if (count($title) > 0) {
0 ignored issues
show
Bug introduced by
$title of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        if (count(/** @scrutinizer ignore-type */ $title) > 0) {
Loading history...
98
            if ($withExt) {
99
                $title .= '.html';
100
            }
101
102
            return $title;
103
        }
104
105
        return '';
106
    }
107
108
    /**
109
     * @param        $op
110
     * @param        $id
111
     * @param string $shortUrl
112
     *
113
     * @return string
114
     */
115
    public static function generateUrl($op, $id, $shortUrl = '')
116
    {
117
        /** @var Publisher\Helper $helper */
118
        $helper = Publisher\Helper::getInstance();
119
        if ('none' !== $helper->getConfig('seo_url_rewrite')) {
120
            if (!empty($shortUrl)) {
121
                $shortUrl .= '.html';
122
            }
123
124
            if ('htaccess' === $helper->getConfig('seo_url_rewrite')) {
125
                // generate SEO url using htaccess
126
                return XOOPS_URL . '/' . $helper->getConfig('seo_module_name') . ".${op}.${id}/${shortUrl}";
127
            }
128
129
            if ('path-info' === $helper->getConfig('seo_url_rewrite')) {
130
                // generate SEO url using path-info
131
                return PUBLISHER_URL . "/index.php/${op}.${id}/${shortUrl}";
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Publisher\PUBLISHER_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
            }
133
            exit('Unknown SEO method.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
134
        }
135
        // generate classic url
136
        switch ($op) {
137
            case 'category':
138
                return PUBLISHER_URL . "/${op}.php?categoryid=${id}";
139
            case 'item':
140
            case 'print':
141
                return PUBLISHER_URL . "/${op}.php?itemid=${id}";
142
            default:
143
                exit('Unknown SEO operation.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
144
        }
145
    }
146
}
147