Passed
Push — master ( df5b6f...0d9957 )
by Mihail
05:31
created

Widgets/Basic/LanguageSwitcher.php (3 issues)

1
<?php
2
3
namespace Widgets\Basic;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Widget;
7
use Ffcms\Core\Helper\HTML\Listing;
0 ignored issues
show
The type Ffcms\Core\Helper\HTML\Listing was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Obj;
10
11
/**
12
 * Class LanguageSwitcher. Show language switched as listing html object
13
 * @package Widgets\Basic
14
 */
15
class LanguageSwitcher extends Widget
16
{
17
    private $multiLangEnabled = false;
18
    private $langs = [];
19
20
    public $css = ['class' => 'list-inline'];
21
    public $onlyArrayItems = false;
22
23
    /**
24
     * Set configurations values in widget attributes
25
     */
26
    public function init()
27
    {
28
        $this->multiLangEnabled = App::$Properties->get('multiLanguage');
29
        $this->langs = App::$Properties->get('languages');
30
31
        if ($this->multiLangEnabled) {
32
            App::$Alias->setCustomLibrary('css', '/vendor/phpffcms/language-flags/flags.css');
0 ignored issues
show
The method setCustomLibrary() does not exist on Ffcms\Core\Alias. ( Ignorable by Annotation )

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

32
            App::$Alias->/** @scrutinizer ignore-call */ 
33
                         setCustomLibrary('css', '/vendor/phpffcms/language-flags/flags.css');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
        }
34
    }
35
36
    /**
37
     * Display language switcher as html or get builded result as array
38
     * @return array|null|string
39
     */
40
    public function display()
41
    {
42
        // prevent loading on disabled multi-language property
43
        if ($this->multiLangEnabled !== true) {
44
            return null;
45
        }
46
47
        // check if languages is defined and count more then 1
48
        if (!Any::isArray($this->langs) || count($this->langs) < 2) {
49
            return null;
50
        }
51
52
        // build output items for listing
53
        $items = [];
54
        foreach ($this->langs as $lang) {
55
            $items[] = [
56
                'type' => 'link',
57
                'link' => App::$Alias->baseUrlNoLang . '/' . $lang . App::$Request->getPathInfo(),
58
                'text' => '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="flag flag-'.$lang.'" alt="'.$lang.'"/>',
59
                'html' => true,
60
                '!secure' => true
61
            ];
62
        }
63
64
        if ($this->onlyArrayItems) {
65
            return $items;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $items returns the type array|array<mixed,array<string,string|true>> which is incompatible with the return type mandated by Ffcms\Core\Arch\Widget::display() of null|string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
66
        }
67
68
        return Listing::display([
69
            'type' => 'ul',
70
            'property' => $this->css,
71
            'items' => $items
72
        ]);
73
    }
74
}
75