GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WorkflowDefinitionImporter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 73
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getImportedWorkflows() 0 22 5
A parseYAMLImport() 0 33 4
1
<?php
2
3
namespace Symbiote\AdvancedWorkflow\Admin;
4
5
use Exception;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\ValidationException;
9
use Symbiote\AdvancedWorkflow\DataObjects\ImportedWorkflowTemplate;
10
use Symbiote\AdvancedWorkflow\Templates\WorkflowTemplate;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Workflow definition import-specific logic. @see {@link WorkflowDefinitionExporter}.
15
 *
16
 * @author  [email protected]
17
 * @license BSD License (http://silverstripe.org/bsd-license/)
18
 * @package advancedworkflow
19
 */
20
class WorkflowDefinitionImporter
21
{
22
    /**
23
     * Generates an array of WorkflowTemplate Objects of all uploaded workflows.
24
     *
25
     * @param string $name. If set, a single-value array comprising a WorkflowTemplate object who's first
0 ignored issues
show
Bug introduced by
There is no parameter named $name.. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     *                      constructor param matches $name is returned.
27
     * @return WorkflowTemplate|WorkflowTemplate[]
28
     */
29
    public function getImportedWorkflows($name = null)
30
    {
31
        $imports = DataObject::get(ImportedWorkflowTemplate::class);
32
        $importedDefs = array();
33
        foreach ($imports as $import) {
34
            if (!$import->Content) {
35
                continue;
36
            }
37
            $structure = unserialize($import->Content);
38
            $struct = $structure[Injector::class]['ExportedWorkflow'];
39
            $template = Injector::inst()->createWithArgs(WorkflowTemplate::class, $struct['constructor']);
40
            $template->setStructure($struct['properties']['structure']);
41
            if ($name) {
42
                if ($struct['constructor'][0] == trim($name)) {
43
                    return $template;
44
                }
45
                continue;
46
            }
47
            $importedDefs[] = $template;
48
        }
49
        return $importedDefs;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $importedDefs; (array) is incompatible with the return type documented by Symbiote\AdvancedWorkflo...r::getImportedWorkflows of type Symbiote\AdvancedWorkflo...ates\WorkflowTemplate[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
50
    }
51
52
    /**
53
     * Handles finding and parsing YAML input as a string or from the contents of a file.
54
     *
55
     * @see addYAMLConfigFile() on {@link SS_ConfigManifest} from where this logic was taken and adapted.
56
     * @param string $source YAML as a string or a filename
57
     * @return array
58
     */
59
    public function parseYAMLImport($source)
60
    {
61
        if (is_file($source)) {
62
            $source = file_get_contents($source);
63
        }
64
65
        // Make sure the linefeeds are all converted to \n, PCRE '$' will not match anything else.
66
        $convertLF = str_replace(array("\r\n", "\r"), "\n", $source);
67
        /*
68
         * Remove illegal colons from Transition/Action titles, otherwise sfYamlParser will barf on them
69
         * Note: The regex relies on there being single quotes wrapped around these in the export .ss template
70
         */
71
        $converted = preg_replace("#('[^:\n][^']+)(:)([^']+')#", "$1;$3", $convertLF);
72
        $parts = preg_split('#^---$#m', $converted, -1, PREG_SPLIT_NO_EMPTY);
73
74
        // If we got an odd number of parts the config, file doesn't have a header.
75
        // We know in advance the number of blocks imported content will have so we settle for a count()==2 check.
76
        if (count($parts) != 2) {
77
            $msg = _t('WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_HEADER', 'Invalid YAML format.');
78
            throw new ValidationException($msg);
79
        }
80
81
        try {
82
            $parsed = Yaml::parse($parts[1]);
83
            return $parsed;
84
        } catch (Exception $e) {
85
            $msg = _t(
86
                'WorkflowDefinitionImporter.INVALID_YML_FORMAT_NO_PARSE',
87
                'Invalid YAML format. Unable to parse.'
88
            );
89
            throw new ValidationException($msg);
90
        }
91
    }
92
}
93