Completed
Pull Request — master (#114)
by Naman
27:16
created

ANetSensitiveFields   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 68.42%

Importance

Changes 11
Bugs 0 Features 6
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 78
ccs 26
cts 38
cp 0.6842
rs 10
c 11
b 0
f 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
C fetchFromConfigFiles() 0 58 9
A getSensitiveStringRegexes() 0 6 2
A getSensitiveXmlTags() 0 6 2
1
<?php
2
namespace net\authorize\util;
3
4
use JMS\Serializer\SerializerBuilder;
5
6 1
define("ANET_SENSITIVE_XMLTAGS_JSON_FILE","AuthorizedNetSensitiveTagsConfig.json");
7 1
define("ANET_SENSITIVE_DATE_CONFIG_CLASS",'net\authorize\util\SensitiveDataConfigType');
8
9
class ANetSensitiveFields
10
{
11
    private static $applySensitiveTags = NULL;
12
    private static $sensitiveStringRegexes = NULL;
13
14 1
    private static function fetchFromConfigFiles(){
15 1
        if(!class_exists(ANET_SENSITIVE_DATE_CONFIG_CLASS))
16
            exit("Class (".ANET_SENSITIVE_DATE_CONFIG_CLASS.") doesn't exist; can't deserialize json; can't log. Exiting.");
0 ignored issues
show
Coding Style Compatibility introduced by
The method fetchFromConfigFiles() 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...
17
        
18 1
        $serializer = SerializerBuilder::create()->build();
19
20 1
        $userConfigFilePath = ANET_SENSITIVE_XMLTAGS_JSON_FILE;
21 1
        $presentUserConfigFile = file_exists($userConfigFilePath);
22
        
23 1
        $configFilePath = dirname(__FILE__) . "/" . ANET_SENSITIVE_XMLTAGS_JSON_FILE;
24 1
        $useDefaultConfigFile = !$presentUserConfigFile;
25
        
26 1
        if ($presentUserConfigFile) { //client config for tags
27
            //read list of tags (and associated regex-patterns and replacements) from .json file
28
            try{
29
                $jsonFileData=file_get_contents($userConfigFilePath);
30
                $sensitiveDataConfig = $serializer->deserialize($jsonFileData, ANET_SENSITIVE_DATE_CONFIG_CLASS, 'json');
31
                
32
                $sensitiveTags = $sensitiveDataConfig->sensitiveTags;
33
                self::$sensitiveStringRegexes = $sensitiveDataConfig->sensitiveStringRegexes;
34
            }
35
            
36
            catch(Exception $e){
0 ignored issues
show
Bug introduced by
The class net\authorize\util\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
37
                echo "ERROR deserializing json from : " . $userConfigFilePath  . "; Exception : " . $e->getMessage(); 
38
                $useDefaultConfigFile = true;
39
            }
40
        }
41
        
42 1
        if ($useDefaultConfigFile) { //default sdk config for tags
43 1
            if(!file_exists($configFilePath)){
44
                exit("ERROR: No config file: " . $configFilePath);
0 ignored issues
show
Coding Style Compatibility introduced by
The method fetchFromConfigFiles() 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...
45
            }
46
            
47
            //read list of tags (and associated regex-patterns and replacements) from .json file
48
            try{
49 1
            $jsonFileData=file_get_contents($configFilePath);
50 1
            $sensitiveDataConfig = $serializer->deserialize($jsonFileData, ANET_SENSITIVE_DATE_CONFIG_CLASS, 'json');
51
            
52 1
            $sensitiveTags = $sensitiveDataConfig->sensitiveTags;
53 1
            self::$sensitiveStringRegexes = $sensitiveDataConfig->sensitiveStringRegexes;
54
            }
55
            
56
            catch(Exception $e){
0 ignored issues
show
Bug introduced by
The class net\authorize\util\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
57
                exit( "ERROR deserializing json from : " . $configFilePath  . "; Exception : " . $e->getMessage()); 
0 ignored issues
show
Coding Style Compatibility introduced by
The method fetchFromConfigFiles() 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...
58
            }
59
        }
60
        
61
        //Check for disableMask flag in case of client json.
62 1
        self::$applySensitiveTags = array();
63 1
        foreach($sensitiveTags as $sensitiveTag){
0 ignored issues
show
Bug introduced by
The variable $sensitiveTags 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...
64 1
            if($sensitiveTag->disableMask){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
65
                //skip masking continue;
66
            }
67
            else{
68 1
                array_push(self::$applySensitiveTags,$sensitiveTag);
69
            }
70
        }
71 1
    }
72
    
73 1
    public static function getSensitiveStringRegexes(){
74 1
        if(NULL == self::$sensitiveStringRegexes) {
75
            self::fetchFromConfigFiles();
76
        }
77 1
        return self::$sensitiveStringRegexes;
78
    }
79
    
80 1
    public static function getSensitiveXmlTags(){
81 1
        if(NULL == self::$applySensitiveTags) {
82 1
            self::fetchFromConfigFiles();
83
        }
84 1
        return self::$applySensitiveTags;
85
    }
86
}
87