| Conditions | 122 |
| Paths | > 20000 |
| Total Lines | 259 |
| Code Lines | 175 |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 187 | function restrictedArea($user, $features, $objectid = 0, $tableandshare = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid', $isdraft = 0) |
||
| 188 | {
|
||
| 189 | global $db, $conf; |
||
| 190 | global $hookmanager; |
||
| 191 | |||
| 192 | //dol_syslog("functions.lib:restrictedArea $feature, $objectid, $dbtablename,$feature2,$dbt_socfield,$dbt_select");
|
||
| 193 | //print "user_id=".$user->id.", features=".$features.", feature2=".$feature2.", objectid=".$objectid; |
||
| 194 | //print ", dbtablename=".$dbtablename.", dbt_socfield=".$dbt_keyfield.", dbt_select=".$dbt_select; |
||
| 195 | //print ", perm: ".$features."->".$feature2."=".($user->rights->$features->$feature2->lire)."<br>"; |
||
| 196 | // Get more permissions checks from hooks |
||
| 197 | $parameters = array('features' => $features, 'objectid' => $objectid, 'idtype' => $dbt_select);
|
||
| 198 | $reshook = $hookmanager->executeHooks('restrictedArea', $parameters);
|
||
| 199 | if (!empty($hookmanager->resArray['result'])) |
||
| 200 | return true; |
||
| 201 | if ($reshook > 0) |
||
| 202 | return false; |
||
| 203 | |||
| 204 | if ($dbt_select != 'rowid' && $dbt_select != 'id') |
||
| 205 | $objectid = "'" . $objectid . "'"; |
||
| 206 | |||
| 207 | // Features/modules to check |
||
| 208 | $featuresarray = array($features); |
||
| 209 | if (preg_match('/&/', $features))
|
||
| 210 | $featuresarray = explode("&", $features);
|
||
| 211 | else if (preg_match('/\|/', $features))
|
||
| 212 | $featuresarray = explode("|", $features);
|
||
| 213 | |||
| 214 | // More subfeatures to check |
||
| 215 | if (!empty($feature2)) |
||
| 216 | $feature2 = explode("|", $feature2);
|
||
| 217 | |||
| 218 | // More parameters |
||
| 219 | $params = explode('&', $tableandshare);
|
||
| 220 | $dbtablename = (!empty($params[0]) ? $params[0] : ''); |
||
| 221 | $sharedelement = (!empty($params[1]) ? $params[1] : $dbtablename); |
||
| 222 | |||
| 223 | $listofmodules = explode(',', Globals::$conf->global->MAIN_MODULES_FOR_EXTERNAL);
|
||
| 224 | |||
| 225 | // Check read permission from module |
||
| 226 | $readok = 1; |
||
| 227 | $nbko = 0; |
||
| 228 | foreach ($featuresarray as $feature) { // first we check nb of test ko
|
||
| 229 | $featureforlistofmodule = $feature; |
||
| 230 | if ($featureforlistofmodule == 'produit') |
||
| 231 | $featureforlistofmodule = 'product'; |
||
| 232 | if (!empty($user->societe_id) && !empty(Globals::$conf->global->MAIN_MODULES_FOR_EXTERNAL) && !in_array($featureforlistofmodule, $listofmodules)) { // If limits on modules for external users, module must be into list of modules for external users
|
||
| 233 | $readok = 0; |
||
| 234 | $nbko++; |
||
| 235 | continue; |
||
| 236 | } |
||
| 237 | |||
| 238 | if ($feature == 'societe') {
|
||
| 239 | if (!$user->rights->societe->lire && !$user->rights->fournisseur->lire) {
|
||
| 240 | $readok = 0; |
||
| 241 | $nbko++; |
||
| 242 | } |
||
| 243 | } else if ($feature == 'contact') {
|
||
| 244 | if (!$user->rights->societe->contact->lire) {
|
||
| 245 | $readok = 0; |
||
| 246 | $nbko++; |
||
| 247 | } |
||
| 248 | } else if ($feature == 'produit|service') {
|
||
| 249 | if (!$user->rights->produit->lire && !$user->rights->service->lire) {
|
||
| 250 | $readok = 0; |
||
| 251 | $nbko++; |
||
| 252 | } |
||
| 253 | } else if ($feature == 'prelevement') {
|
||
| 254 | if (!$user->rights->prelevement->bons->lire) {
|
||
| 255 | $readok = 0; |
||
| 256 | $nbko++; |
||
| 257 | } |
||
| 258 | } else if ($feature == 'cheque') {
|
||
| 259 | if (!$user->rights->banque->cheque) {
|
||
| 260 | $readok = 0; |
||
| 261 | $nbko++; |
||
| 262 | } |
||
| 263 | } else if ($feature == 'projet') {
|
||
| 264 | if (!$user->rights->projet->lire && !$user->rights->projet->all->lire) {
|
||
| 265 | $readok = 0; |
||
| 266 | $nbko++; |
||
| 267 | } |
||
| 268 | } else if (!empty($feature2)) { // This should be used for future changes
|
||
| 269 | $tmpreadok = 1; |
||
| 270 | foreach ($feature2 as $subfeature) {
|
||
| 271 | if (!empty($subfeature) && empty($user->rights->$feature->$subfeature->lire) && empty($user->rights->$feature->$subfeature->read)) {
|
||
| 272 | $tmpreadok = 0; |
||
| 273 | } else if (empty($subfeature) && empty($user->rights->$feature->lire) && empty($user->rights->$feature->read)) {
|
||
| 274 | $tmpreadok = 0; |
||
| 275 | } else {
|
||
| 276 | $tmpreadok = 1; |
||
| 277 | break; |
||
| 278 | } // Break is to bypass second test if the first is ok |
||
| 279 | } |
||
| 280 | if (!$tmpreadok) { // We found a test on feature that is ko
|
||
| 281 | $readok = 0; // All tests are ko (we manage here the and, the or will be managed later using $nbko). |
||
| 282 | $nbko++; |
||
| 283 | } |
||
| 284 | } else if (!empty($feature) && ($feature != 'user' && $feature != 'usergroup')) { // This is for old permissions
|
||
| 285 | if (empty($user->rights->$feature->lire) && empty($user->rights->$feature->read) && empty($user->rights->$feature->run)) {
|
||
| 286 | $readok = 0; |
||
| 287 | $nbko++; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | // If a or and at least one ok |
||
| 293 | if (preg_match('/\|/', $features) && $nbko < count($featuresarray))
|
||
| 294 | $readok = 1; |
||
| 295 | |||
| 296 | if (!$readok) |
||
| 297 | accessforbidden(); |
||
| 298 | //print "Read access is ok"; |
||
| 299 | // Check write permission from module (we need to know write permission to create but also to delete drafts record) |
||
| 300 | $createok = 1; |
||
| 301 | $nbko = 0; |
||
| 302 | if (GETPOST('action', 'aZ09') == 'create' || ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete')) {
|
||
| 303 | foreach ($featuresarray as $feature) {
|
||
| 304 | if ($feature == 'contact') {
|
||
| 305 | if (!$user->rights->societe->contact->creer) {
|
||
| 306 | $createok = 0; |
||
| 307 | $nbko++; |
||
| 308 | } |
||
| 309 | } else if ($feature == 'produit|service') {
|
||
| 310 | if (!$user->rights->produit->creer && !$user->rights->service->creer) {
|
||
| 311 | $createok = 0; |
||
| 312 | $nbko++; |
||
| 313 | } |
||
| 314 | } else if ($feature == 'prelevement') {
|
||
| 315 | if (!$user->rights->prelevement->bons->creer) {
|
||
| 316 | $createok = 0; |
||
| 317 | $nbko++; |
||
| 318 | } |
||
| 319 | } else if ($feature == 'commande_fournisseur') {
|
||
| 320 | if (!$user->rights->fournisseur->commande->creer) {
|
||
| 321 | $createok = 0; |
||
| 322 | $nbko++; |
||
| 323 | } |
||
| 324 | } else if ($feature == 'banque') {
|
||
| 325 | if (!$user->rights->banque->modifier) {
|
||
| 326 | $createok = 0; |
||
| 327 | $nbko++; |
||
| 328 | } |
||
| 329 | } else if ($feature == 'cheque') {
|
||
| 330 | if (!$user->rights->banque->cheque) {
|
||
| 331 | $createok = 0; |
||
| 332 | $nbko++; |
||
| 333 | } |
||
| 334 | } else if (!empty($feature2)) { // This should be used
|
||
| 335 | foreach ($feature2 as $subfeature) {
|
||
| 336 | if (empty($user->rights->$feature->$subfeature->creer) && empty($user->rights->$feature->$subfeature->write) && empty($user->rights->$feature->$subfeature->create)) {
|
||
| 337 | $createok = 0; |
||
| 338 | $nbko++; |
||
| 339 | } else {
|
||
| 340 | $createok = 1; |
||
| 341 | break; |
||
| 342 | } // Break to bypass second test if the first is ok |
||
| 343 | } |
||
| 344 | } else if (!empty($feature)) { // This is for old permissions ('creer' or 'write')
|
||
| 345 | //print '<br>feature='.$feature.' creer='.$user->rights->$feature->creer.' write='.$user->rights->$feature->write; |
||
| 346 | if (empty($user->rights->$feature->creer) && empty($user->rights->$feature->write) && empty($user->rights->$feature->create)) {
|
||
| 347 | $createok = 0; |
||
| 348 | $nbko++; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | // If a or and at least one ok |
||
| 354 | if (preg_match('/\|/', $features) && $nbko < count($featuresarray))
|
||
| 355 | $createok = 1; |
||
| 356 | |||
| 357 | if (GETPOST('action', 'aZ09') == 'create' && !$createok)
|
||
| 358 | accessforbidden(); |
||
| 359 | //print "Write access is ok"; |
||
| 360 | } |
||
| 361 | |||
| 362 | // Check create user permission |
||
| 363 | $createuserok = 1; |
||
| 364 | if (GETPOST('action', 'aZ09') == 'confirm_create_user' && GETPOST("confirm", 'aZ09') == 'yes') {
|
||
| 365 | if (!$user->rights->user->user->creer) |
||
| 366 | $createuserok = 0; |
||
| 367 | |||
| 368 | if (!$createuserok) |
||
| 369 | accessforbidden(); |
||
| 370 | //print "Create user access is ok"; |
||
| 371 | } |
||
| 372 | |||
| 373 | // Check delete permission from module |
||
| 374 | $deleteok = 1; |
||
| 375 | $nbko = 0; |
||
| 376 | if ((GETPOST("action", "aZ09") == 'confirm_delete' && GETPOST("confirm", "aZ09") == 'yes') || GETPOST("action", "aZ09") == 'delete') {
|
||
| 377 | foreach ($featuresarray as $feature) {
|
||
| 378 | if ($feature == 'contact') {
|
||
| 379 | if (!$user->rights->societe->contact->supprimer) |
||
| 380 | $deleteok = 0; |
||
| 381 | } |
||
| 382 | else if ($feature == 'produit|service') {
|
||
| 383 | if (!$user->rights->produit->supprimer && !$user->rights->service->supprimer) |
||
| 384 | $deleteok = 0; |
||
| 385 | } |
||
| 386 | else if ($feature == 'commande_fournisseur') {
|
||
| 387 | if (!$user->rights->fournisseur->commande->supprimer) |
||
| 388 | $deleteok = 0; |
||
| 389 | } |
||
| 390 | else if ($feature == 'banque') {
|
||
| 391 | if (!$user->rights->banque->modifier) |
||
| 392 | $deleteok = 0; |
||
| 393 | } |
||
| 394 | else if ($feature == 'cheque') {
|
||
| 395 | if (!$user->rights->banque->cheque) |
||
| 396 | $deleteok = 0; |
||
| 397 | } |
||
| 398 | else if ($feature == 'ecm') {
|
||
| 399 | if (!$user->rights->ecm->upload) |
||
| 400 | $deleteok = 0; |
||
| 401 | } |
||
| 402 | else if ($feature == 'ftp') {
|
||
| 403 | if (!$user->rights->ftp->write) |
||
| 404 | $deleteok = 0; |
||
| 405 | }else if ($feature == 'salaries') {
|
||
| 406 | if (!$user->rights->salaries->delete) |
||
| 407 | $deleteok = 0; |
||
| 408 | } |
||
| 409 | else if ($feature == 'salaries') {
|
||
| 410 | if (!$user->rights->salaries->delete) |
||
| 411 | $deleteok = 0; |
||
| 412 | } |
||
| 413 | else if (!empty($feature2)) { // This should be used for permissions on 2 levels
|
||
| 414 | foreach ($feature2 as $subfeature) {
|
||
| 415 | if (empty($user->rights->$feature->$subfeature->supprimer) && empty($user->rights->$feature->$subfeature->delete)) |
||
| 416 | $deleteok = 0; |
||
| 417 | else {
|
||
| 418 | $deleteok = 1; |
||
| 419 | break; |
||
| 420 | } // For bypass the second test if the first is ok |
||
| 421 | } |
||
| 422 | } else if (!empty($feature)) { // This is used for permissions on 1 level
|
||
| 423 | //print '<br>feature='.$feature.' creer='.$user->rights->$feature->supprimer.' write='.$user->rights->$feature->delete; |
||
| 424 | if (empty($user->rights->$feature->supprimer) && empty($user->rights->$feature->delete) && empty($user->rights->$feature->run)) |
||
| 425 | $deleteok = 0; |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | // If a or and at least one ok |
||
| 430 | if (preg_match('/\|/', $features) && $nbko < count($featuresarray))
|
||
| 431 | $deleteok = 1; |
||
| 432 | |||
| 433 | if (!$deleteok && !($isdraft && $createok)) |
||
| 434 | accessforbidden(); |
||
| 435 | //print "Delete access is ok"; |
||
| 436 | } |
||
| 437 | |||
| 438 | // If we have a particular object to check permissions on, we check this object |
||
| 439 | // is linked to a company allowed to $user. |
||
| 440 | if (!empty($objectid) && $objectid > 0) {
|
||
| 441 | $ok = checkUserAccessToObject($user, $featuresarray, $objectid, $tableandshare, $feature2, $dbt_keyfield, $dbt_select); |
||
| 442 | return $ok ? 1 : accessforbidden(); |
||
| 443 | } |
||
| 444 | |||
| 445 | return 1; |
||
| 446 | } |
||
| 694 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.