Completed
Push — feature-assets ( 7e7631 )
by Arnaud
04:12
created

Parsedown   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B inlineImage() 0 40 6
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Converter;
10
11
use Cecil\Config;
12
use Cecil\Util;
13
use Intervention\Image\ImageManagerStatic as Image;
14
use ParsedownExtra;
15
16
class Parsedown extends ParsedownExtra
17
{
18
    // https://regex101.com/r/EhIh5N/2
19
    const PATTERN = '(.*)(\?|\&)([^=]+)\=([^&]+)';
20
    private $config;
21
22
    function __construct(Config $config = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        $this->config = $config;
25
    }
26
27
    protected function inlineImage($excerpt)
28
    {
29
        $image = parent::inlineImage($excerpt);
30
31
        if (!isset($image)) {
32
            return null;
33
        }
34
35
        preg_match(
36
            '/'.self::PATTERN.'/s',
37
            $image['element']['attributes']['src'],
38
            $matches
39
        );
40
41
        if (!$matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            return $image;
43
        }
44
45
        if (key_exists(3, $matches) && $matches[3] == 'resize') {
46
            $image['element']['attributes']['src'] = $matches[1];
47
            $resize = $matches[4];
48
        }
49
50
        if ($this->config === null) {
51
            return $image;
52
        }
53
54
        Util::getFS()->mkdir($this->config->getOutputPath().'/assets');
55
56
        Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src'])
57
            ->resize($resize, null, function ($constraint) {
0 ignored issues
show
Bug introduced by
The variable $resize 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...
58
                $constraint->aspectRatio();
59
                $constraint->upsize();
60
            })
61
            ->save($this->config->getOutputPath(). '/assets'.$image['element']['attributes']['src']);
62
63
        $image['element']['attributes']['src'] = '/assets'.$image['element']['attributes']['src'];
64
65
        return $image;
66
    }
67
}
68