ImpressaoFiscalController::cabecalho()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class ImpressaoFiscalController extends AppController {
4
5
	protected $txt;
6
	protected $userName;
7
8
	public function gerar_arquivo() {
9
		$this->cabecalho();
10
		
11
		$this->corpo();
12
13
		$this->rodape();
0 ignored issues
show
Unused Code introduced by
The call to the method ImpressaoFiscalController::rodape() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
14
15
		$name = date('Y-m-d') . uniqid() . ".txt";
16
		
17
		$dir = APP . 'webroot/uploads/venda/fiscal/';
18
		
19
		$nameFull = $dir . $name;
20
21
		$file = fopen($nameFull, "w") or die("Não foi possivel imprimir arquivo!");
0 ignored issues
show
Coding Style Compatibility introduced by
The method gerar_arquivo() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
22
				
23
		fwrite($file, $this->txt);
24
		
25
		fclose($file);
26
27
		return $name;
28
	}
29
30
	public function cabecalho() {
31
		$this->txt .= "     Winners OpenSource    \n";
32
		$this->txt .= "--------------------------\n";
33
		$this->txt .= "  " . $this->userName .   "\n";
34
		$this->txt .= "--------------------------\n";
35
		
36
		$this->txt .= "Data: " . date('d')  . '/' . date('m') . '/' . date('Y')
37
				   . "\nHora: " . date('H:i:s') . "\n";
38
39
		$this->txt .= "--------------------------\n\n";
40
	}
41
42
	public function rodape() {
43
		return;
44
	}
45
46
	public function corpo() {
47
		if (!isset($this->corpoTxt) || empty($this->corpoTxt))
48
			return false;
49
50
		$this->txt .= $this->corpoTxt;
0 ignored issues
show
Documentation introduced by
The property corpoTxt does not exist on object<ImpressaoFiscalController>. Since you implemented __set, maybe consider adding a @property annotation.

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...
51
52
		return $this->corpoTxt;
53
	}
54
55
	public function exibir() {
0 ignored issues
show
Coding Style introduced by
exibir uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
56
		$arquivo = $_GET['arquivo'];
57
		
58
		echo file_get_contents(APP . 'webroot/uploads/venda/fiscal/' . $arquivo);
59
		
60
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method exibir() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
61
	}
62
63
}