Passed
Push — developer ( 8ac017...cc24b2 )
by Mariusz
73:39 queued 38:36
created

Viewer::truncateText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
/**
3
 * The file contains: Base controller class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
/**
15
 * Base controller class.
16
 */
17
class Viewer extends \SmartyBC
18
{
19
	const DEFAULTLAYOUT = 'Default';
20
21
	public static $currentLayout;
22
23
	/**
24
	 * Constructor - Sets the templateDir and compileDir for the Smarty files.
25
	 *
26
	 * @param <String> - $media Layout/Media name
27
	 */
28
	public function __construct($media = '')
0 ignored issues
show
Unused Code introduced by
The parameter $media is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
	{
30
		parent::__construct();
31
32
		self::$currentLayout = self::getLayoutName();
33
		$templatesDir = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName();
34
		$customTemplate = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'custom' . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName();
35
		$compileDir = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'cache' . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName();
36
37
		if (!file_exists($compileDir)) {
38
			mkdir($compileDir, 0777, true);
39
		}
40
		$this->setTemplateDir([$customTemplate, $templatesDir]);
41
		$this->setCompileDir($compileDir);
42
	}
43
44
	/**
45
	 * Function to return for default layout name.
46
	 *
47
	 * @return string - Default Layout Name
48
	 */
49
	public static function getLayoutName(): string
50
	{
51
		return Config::$theme ?? self::DEFAULTLAYOUT;
52
	}
53
54
	/**
55
	 * Static function to get the Instance of the Class Object.
56
	 *
57
	 * @param <String> $media Layout/Media
0 ignored issues
show
Documentation introduced by
The doc-type <String> could not be parsed: Unknown type name "<" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
58
	 *
59
	 * @return Viewer instance
60
	 */
61
	public static function getInstance(string $media = '')
62
	{
63
		return new self($media);
64
	}
65
66
	/**
67
	 * Function to display/fetch the smarty file contents.
68
	 *
69
	 * @param string $templateName
70
	 * @param string $moduleName
71
	 * @param bool   $fetch
72
	 *
73
	 * @return string|true - html data
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
	 */
75
	public function view(string $templateName, string $moduleName = '', bool $fetch = false)
76
	{
77
		$templatePath = $this->getTemplatePath($templateName, $moduleName);
78
		$templateFound = $this->templateExists($templatePath);
79
		if ($templateFound) {
80
			if ($fetch) {
81
				return $this->fetch($templatePath);
82
			}
83
			$this->display($templatePath);
84
			return true;
85
		}
86
		throw new Exceptions\AppException("LBL_FILE_TEMPLATE_NOT_FOUND||{$templatePath}");
87
	}
88
89
	/**
90
	 * Function to get the module specific template path for a given template.
91
	 *
92
	 * @param string $templateName
93
	 * @param string $moduleName
94
	 *
95
	 * @return string - Module specific template path if exists, otherwise default template path for the given template name
96
	 */
97
	public function getTemplatePath(string $templateName, string $moduleName = ''): string
98
	{
99
		foreach ($this->getTemplateDir() as $templateDir) {
0 ignored issues
show
Bug introduced by
The expression $this->getTemplateDir() of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
100
			$filePath = 'modules' . \DIRECTORY_SEPARATOR . $moduleName . \DIRECTORY_SEPARATOR . $templateName;
101
			$completeFilePath = $templateDir . $filePath;
102
			if (!empty($moduleName) && file_exists($completeFilePath)) {
103
				break;
104
			}
105
			$filePath = 'modules' . \DIRECTORY_SEPARATOR . 'Base' . \DIRECTORY_SEPARATOR . $templateName;
106
			if (file_exists($templateDir . $filePath)) {
107
				break;
108
			}
109
		}
110
		return $filePath;
0 ignored issues
show
Bug introduced by
The variable $filePath does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
111
	}
112
113
	/**
114
	 * Truncating plain text and adding a button showing all the text.
115
	 *
116
	 * @param string $text
117
	 * @param int    $length
118
	 * @param bool   $showIcon
119
	 *
120
	 * @return string
121
	 */
122
	public static function truncateText(string $text, int $length, bool $showIcon = false): string
123
	{
124
		if (\mb_strlen($text) < $length) {
125
			return $text;
126
		}
127
		$teaser = TextParser::textTruncate($text, $length);
128
		$text = nl2br($text);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $text. This often makes code more readable.
Loading history...
129
		if ($showIcon) {
130
			$btn = '<span class="mdi mdi-overscan"></span>';
131
		} else {
132
			$btn = \App\Language::translate('LBL_MORE_BTN');
133
		}
134
		return "<div class=\"js-more-content\"><span class=\"teaserContent\">$teaser</span><span class=\"fullContent d-none\">$text</span><span class=\"text-right mb-1\"><button type=\"button\" class=\"btn btn-link btn-sm pt-0 js-more\">{$btn}</button></span></div>";
135
	}
136
}
137