Conditions | 5 |
Paths | 10 |
Total Lines | 101 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
65 | protected function prepVariables($provider = null) |
||
66 | { |
||
67 | $variables = $this->getBaseVariables(); |
||
68 | |||
69 | $variables['title'] = Craft::t( |
||
70 | $this->getSamlPlugin()->getHandle(), |
||
71 | $this->getSamlPlugin()->name |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * TYPES |
||
76 | */ |
||
77 | $variables['myType'] = $this->getSamlPlugin()->getMyType(); |
||
78 | $variables['remoteType'] = $this->getSamlPlugin()->getRemoteType(); |
||
79 | $variables['createType'] = $variables['remoteType']; |
||
80 | |||
81 | if ($provider) { |
||
82 | /** |
||
83 | * @var ProviderInterface $provider |
||
84 | */ |
||
85 | $provider = $variables['provider'] = ( |
||
86 | /** |
||
87 | * Is instance provider |
||
88 | */ |
||
89 | $provider instanceof ProviderInterface ? |
||
90 | $provider : |
||
91 | $provider = $this->getSamlPlugin()->getProviderRecordClass()::find()->where([ |
||
92 | /** |
||
93 | * Is ID |
||
94 | */ |
||
95 | 'id' => $provider, |
||
96 | ])->one() |
||
97 | ); |
||
98 | |||
99 | $variables['title'] .= ': Edit'; |
||
100 | |||
101 | $crumb = [ |
||
102 | [ |
||
103 | |||
104 | 'url' => UrlHelper::cpUrl( |
||
105 | implode( |
||
106 | '/', |
||
107 | [ |
||
108 | $this->getSamlPlugin()->getHandle(), |
||
109 | 'metadata', |
||
110 | ] |
||
111 | ) |
||
112 | ), |
||
113 | 'label' => 'Provider List', |
||
114 | ], [ |
||
115 | 'url' => UrlHelper::cpUrl( |
||
116 | $this->getSamlPlugin()->getHandle() . '/metadata/' . $provider->id |
||
117 | ), |
||
118 | 'label' => $provider->label ?: $provider->entityId, |
||
119 | ] |
||
120 | ]; |
||
121 | $variables['keypair'] = $provider->keychain; |
||
122 | |||
123 | $variables = array_merge( |
||
124 | $variables, |
||
125 | $this->addUrls($provider) |
||
126 | ); |
||
127 | } else { |
||
128 | $record = $this->getSamlPlugin()->getProviderRecordClass(); |
||
129 | |||
130 | $provider = $variables['provider'] = new $record([ |
||
131 | 'providerType' => 'idp', |
||
132 | ]); |
||
133 | |||
134 | $variables['title'] .= ': Create'; |
||
135 | |||
136 | $crumb = [ |
||
137 | [ |
||
138 | 'url' => UrlHelper::cpUrl( |
||
139 | $this->getSamlPlugin()->getHandle() . '/new' |
||
140 | ), |
||
141 | 'label' => 'New', |
||
142 | ] |
||
143 | ]; |
||
144 | } |
||
145 | |||
146 | $variables['allkeypairs'] = []; |
||
147 | |||
148 | $keypairs = KeyChain::getInstance()->getService()->findByPlugin($this->getSamlPlugin())->all(); |
||
149 | |||
150 | foreach ($keypairs as $keypair) { |
||
151 | $variables['allkeypairs'][] = [ |
||
152 | 'label' => $keypair->description, |
||
153 | 'value' => $keypair->id, |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | $variables['crumbs'] = array_merge([ |
||
158 | [ |
||
159 | 'url' => UrlHelper::cpUrl($this->getSamlPlugin()->getHandle()), |
||
160 | 'label' => Craft::t($this->getSamlPlugin()->getHandle(), $this->getSamlPlugin()->name), |
||
161 | ], |
||
162 | ], $crumb); |
||
163 | |||
164 | return $variables; |
||
165 | } |
||
166 | } |
||
167 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: