Completed
Push — rework ( d09de3...810325 )
by Markus
03:02
created

BrowserHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SSpkS\Handler;
4
5
use \SSpkS\Device\DeviceList;
6
use \SSpkS\Output\UrlFixer;
7
use \SSpkS\Package\PackageFinder;
8
use \SSpkS\Package\PackageFilter;
9
10
// if ($_SERVER['REQUEST_METHOD'] == 'GET')
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
11
12
class BrowserHandler implements HandlerInterface
13
{
14
    private $config;
15
16
    public function __construct(\SSpkS\Config $config)
17
    {
18
        $this->config = $config;
19
    }
20
21
    public function handle()
22
    {
23
        // GET-request, probably browser --> show HTML
24
        $arch     = trim($_GET['arch']);
25
        $channel  = trim($_GET['channel']);
26
        if ($channel != 'beta') {
27
            $channel = 'stable';
28
        }
29
        $fullList = trim($_GET['fulllist']);
30
        $packagesAvailable = array();
0 ignored issues
show
Unused Code introduced by
$packagesAvailable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
32
        $mustache = new \Mustache_Engine(array(
33
            'loader'          => new \Mustache_Loader_FilesystemLoader($this->config->basePath . '/data/templates'),
0 ignored issues
show
Documentation introduced by
The property $basePath is declared private in SSpkS\Config. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
34
            'partials_loader' => new \Mustache_Loader_FilesystemLoader($this->config->basePath . '/data/templates/partials'),
0 ignored issues
show
Documentation introduced by
The property $basePath is declared private in SSpkS\Config. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
            'charset'         => 'utf-8',
36
            'logger'          => new \Mustache_Logger_StreamLogger('php://stderr'),
37
        ));
38
39
        $tpl_vars = array(
40
            'siteName'   => $this->config->site['name'],
41
            'arch'       => $arch,
42
            'channel'    => ($channel == 'beta'),
43
            'requestUri' => $_SERVER['REQUEST_URI'],
44
            'baseUrl'    => $this->config->baseUrl,
0 ignored issues
show
Documentation introduced by
The property baseUrl does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
            'fullList'   => $fullList,
46
        );
47
48
        if ($arch) {
49
            // Architecture is set --> show packages for that arch
50
            $pkgs = new PackageFinder($this->config->paths['packages']);
51
            $pkgf = new PackageFilter($pkgs->getAllPackages());
52
            $pkgf->setArchitectureFilter($arch);
53
            $pkgf->setChannelFilter($channel);
54
            $pkgf->setFirmwareVersionFilter(false);
55
            $pkgf->setOldVersionFilter(true);
56
            $filteredPkgList = $pkgf->getFilteredPackageList();
57
58
            $uf = new UrlFixer($this->config->baseUrl);
0 ignored issues
show
Documentation introduced by
The property baseUrl does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
59
            $uf->fixPackageList($filteredPkgList);
60
61
            $packages = array();
62
            foreach ($filteredPkgList as $pkg) {
63
                $packages[] = $pkg->getMetadata();
64
            }
65
66
            $tpl_vars['packagelist'] = array_values($packages);
67
            $tpl = $mustache->loadTemplate('html_packagelist');
68
        } elseif ($fullList) {
69
            // No architecture, but full list of packages requested --> show simple list
70
            $pkgs = new PackageFinder($this->config->paths['packages']);
71
            $packagesList = $pkgs->getAllPackageFiles();
72
73
            // Prepare data for template
74
            $packages = array();
75
            foreach ($packagesList as $spkFile) {
76
                $packages[] = array(
77
                    'url'      => $baseUrl . $spkFile,
0 ignored issues
show
Bug introduced by
The variable $baseUrl does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
78
                    'filename' => basename($spkFile),
79
                );
80
            }
81
            $tpl_vars['packagelist'] = $packages;
82
            $tpl = $mustache->loadTemplate('html_packagelist_all');
83
        } else {
84
            // Nothing requested --> show models overview
85
            try {
86
                $deviceList = new DeviceList($this->config->paths['models']);
87
                $models = $deviceList->getDevices();
88
                if (count($models) == 0) {
89
                    $tpl = $mustache->loadTemplate('html_modellist_none');
90
                } else {
91
                    $tpl_vars['modellist'] = $models;
92
                    $tpl = $mustache->loadTemplate('html_modellist');
93
                }
94
            } catch (\Exception $e) {
95
                $tpl_vars['errorMessage'] = $e->getMessage();
96
                $tpl = $mustache->loadTemplate('html_modellist_error');
97
            }
98
        }
99
        echo $tpl->render($tpl_vars);
100
    }
101
}
102