Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php | ||
| 42 | class SharedAccessSignatureHelper | ||
| 43 | { | ||
| 44 | protected $accountName; | ||
| 45 | protected $accountKey; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Constructor. | ||
| 49 | * | ||
| 50 | * @param string $accountName the name of the storage account. | ||
| 51 | * @param string $accountKey the shared key of the storage account | ||
| 52 | * | ||
| 53 | * @return | ||
|  | |||
| 54 | * MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper | ||
| 55 | */ | ||
| 56 | public function __construct($accountName, $accountKey) | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Helper function to generate a service shared access signature. | ||
| 70 | * | ||
| 71 | * This only supports version 2015-04-05 and later. | ||
| 72 | * | ||
| 73 | * @param string $signedService The service type of the SAS. | ||
| 74 | * @param string $signedResource Resource name to generate the | ||
| 75 | * canonicalized resource. | ||
| 76 | * @param string $resourceName The name of the resource. | ||
| 77 | * @param string $signedPermissions Signed permissions. | ||
| 78 | * @param string $signedExpiry Signed expiry date. | ||
| 79 | * @param string $signedStart Signed start date. | ||
| 80 | * @param string $signedIP Signed IP address. | ||
| 81 | * @param string $signedProtocol Signed protocol. | ||
| 82 | * @param string $signedIdentifier Signed identifier. | ||
| 83 | * @param string $cacheControl Cache-Control header (rscc). | ||
| 84 | * @param string $contentDisposition Content-Disposition header (rscd). | ||
| 85 | * @param string $contentEncoding Content-Encoding header (rsce). | ||
| 86 | * @param string $contentLanguage Content-Language header (rscl). | ||
| 87 | * @param string $contentType Content-Type header (rsct). | ||
| 88 | * @param string $startingPartitionKey Minimum partition key. | ||
| 89 | * @param string $startingRowKey Minimum row key. | ||
| 90 | * @param string $endingPartitionKey Maximum partition key. | ||
| 91 | * @param string $endingRowKey Maximum row key. | ||
| 92 | * | ||
| 93 | * @see Constructing an service SAS at | ||
| 94 | * https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas | ||
| 95 | * @return string | ||
| 96 | */ | ||
| 97 | private function generateServiceSharedAccessSignatureToken( | ||
| 237 | |||
| 238 | /** | ||
| 239 | * Generates Blob service shared access signature. | ||
| 240 | * | ||
| 241 | * This only supports version 2015-04-05 and later. | ||
| 242 | * | ||
| 243 | * @param string $signedResource Resource name to generate the | ||
| 244 | * canonicalized resource. | ||
| 245 | * It can be Resources::RESOURCE_TYPE_BLOB | ||
| 246 | * or Resources::RESOURCE_TYPE_CONTAINER. | ||
| 247 | * @param string $resourceName The name of the resource, including | ||
| 248 | * the path of the resource. It should be | ||
| 249 |      *                                    - {container}/{blob}: for blobs, | ||
| 250 |      *                                    - {container}: for containers, e.g.: | ||
| 251 | * /mymusic/music.mp3 or | ||
| 252 | * music.mp3 | ||
| 253 | * @param string $signedPermissions Signed permissions. | ||
| 254 | * @param string $signedExpiry Signed expiry date. | ||
| 255 | * @param string $signedStart Signed start date. | ||
| 256 | * @param string $signedIP Signed IP address. | ||
| 257 | * @param string $signedProtocol Signed protocol. | ||
| 258 | * @param string $signedIdentifier Signed identifier. | ||
| 259 | * @param string $cacheControl Cache-Control header (rscc). | ||
| 260 | * @param string $contentDisposition Content-Disposition header (rscd). | ||
| 261 | * @param string $contentEncoding Content-Encoding header (rsce). | ||
| 262 | * @param string $contentLanguage Content-Language header (rscl). | ||
| 263 | * @param string $contentType Content-Type header (rsct). | ||
| 264 | * | ||
| 265 | * @see Constructing an service SAS at | ||
| 266 | * https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas | ||
| 267 | * @return string | ||
| 268 | */ | ||
| 269 | View Code Duplication | public function generateBlobServiceSharedAccessSignatureToken( | |
| 318 | |||
| 319 | /** | ||
| 320 | * Generates File service shared access signature. | ||
| 321 | * | ||
| 322 | * This only supports version 2015-04-05 and later. | ||
| 323 | * | ||
| 324 | * @param string $signedResource Resource name to generate the | ||
| 325 | * canonicalized resource. | ||
| 326 | * It can be Resources::RESOURCE_TYPE_FILE | ||
| 327 | * or Resources::RESOURCE_TYPE_SHARE. | ||
| 328 | * @param string $resourceName The name of the resource, including | ||
| 329 | * the path of the resource. It should be | ||
| 330 |      *                                    - {share}/{file}: for files, | ||
| 331 |      *                                    - {share}: for shares, e.g.: | ||
| 332 | * /mymusic/music.mp3 or | ||
| 333 | * music.mp3 | ||
| 334 | * @param string $signedPermissions Signed permissions. | ||
| 335 | * @param string $signedExpiry Signed expiry date. | ||
| 336 | * @param string $signedStart Signed start date. | ||
| 337 | * @param string $signedIP Signed IP address. | ||
| 338 | * @param string $signedProtocol Signed protocol. | ||
| 339 | * @param string $signedIdentifier Signed identifier. | ||
| 340 | * @param string $cacheControl Cache-Control header (rscc). | ||
| 341 | * @param string $contentDisposition Content-Disposition header (rscd). | ||
| 342 | * @param string $contentEncoding Content-Encoding header (rsce). | ||
| 343 | * @param string $contentLanguage Content-Language header (rscl). | ||
| 344 | * @param string $contentType Content-Type header (rsct). | ||
| 345 | * | ||
| 346 | * @see Constructing an service SAS at | ||
| 347 | * https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas | ||
| 348 | * @return string | ||
| 349 | */ | ||
| 350 | View Code Duplication | public function generateFileServiceSharedAccessSignatureToken( | |
| 399 | |||
| 400 | /** | ||
| 401 | * Generates Table service shared access signature. | ||
| 402 | * | ||
| 403 | * This only supports version 2015-04-05 and later. | ||
| 404 | * | ||
| 405 | * @param string $tableName The name of the table. | ||
| 406 | * @param string $signedPermissions Signed permissions. | ||
| 407 | * @param string $signedExpiry Signed expiry date. | ||
| 408 | * @param string $signedStart Signed start date. | ||
| 409 | * @param string $signedIP Signed IP address. | ||
| 410 | * @param string $signedProtocol Signed protocol. | ||
| 411 | * @param string $signedIdentifier Signed identifier. | ||
| 412 | * @param string $startingPartitionKey Minimum partition key. | ||
| 413 | * @param string $startingRowKey Minimum row key. | ||
| 414 | * @param string $endingPartitionKey Maximum partition key. | ||
| 415 | * @param string $endingRowKey Maximum row key. | ||
| 416 | * | ||
| 417 | * @see Constructing an service SAS at | ||
| 418 | * https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas | ||
| 419 | * @return string | ||
| 420 | */ | ||
| 421 | public function generateTableServiceSharedAccessSignatureToken( | ||
| 455 | |||
| 456 | /** | ||
| 457 | * Generates a queue service shared access signature. | ||
| 458 | * | ||
| 459 | * This only supports version 2015-04-05 and later. | ||
| 460 | * | ||
| 461 | * @param string $queueName The name of the queue. | ||
| 462 | * @param string $signedPermissions Signed permissions. | ||
| 463 | * @param string $signedExpiry Signed expiry date. | ||
| 464 | * @param string $signedStart Signed start date. | ||
| 465 | * @param string $signedIdentifier Signed identifier. | ||
| 466 | * @param string $signedIP Signed IP address. | ||
| 467 | * @param string $signedProtocol Signed protocol. | ||
| 468 | * | ||
| 469 | * @see Constructing an service SAS at | ||
| 470 | * https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas | ||
| 471 | * @return string | ||
| 472 | */ | ||
| 473 | public function generateQueueServiceSharedAccessSignatureToken( | ||
| 503 | |||
| 504 | /** | ||
| 505 | * Generates a shared access signature at the account level. | ||
| 506 | * | ||
| 507 | * @param string $signedVersion Specifies the signed version to use. | ||
| 508 | * @param string $signedPermissions Specifies the signed permissions for | ||
| 509 | * the account SAS. | ||
| 510 | * @param string $signedService Specifies the signed services | ||
| 511 | * accessible with the account SAS. | ||
| 512 | * @param string $signedResourceType Specifies the signed resource types | ||
| 513 | * that are accessible with the account | ||
| 514 | * SAS. | ||
| 515 | * @param string $signedExpiry The time at which the shared access | ||
| 516 | * signature becomes invalid, in an ISO | ||
| 517 | * 8601 format. | ||
| 518 | * @param string $signedStart The time at which the SAS becomes | ||
| 519 | * valid, in an ISO 8601 format. | ||
| 520 | * @param string $signedIP Specifies an IP address or a range | ||
| 521 | * of IP addresses from which to accept | ||
| 522 | * requests. | ||
| 523 | * @param string $signedProtocol Specifies the protocol permitted for | ||
| 524 | * a request made with the account SAS. | ||
| 525 | * | ||
| 526 | * @see Constructing an account SAS at | ||
| 527 | * https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/constructing-an-account-sas | ||
| 528 | * | ||
| 529 | * @return string | ||
| 530 | */ | ||
| 531 | public function generateAccountSharedAccessSignatureToken( | ||
| 610 | |||
| 611 | /** | ||
| 612 | * Validates and sanitizes the signed service parameter | ||
| 613 | * | ||
| 614 | * @param string $signedService Specifies the signed services accessible | ||
| 615 | * with the account SAS. | ||
| 616 | * | ||
| 617 | * @return string | ||
| 618 | */ | ||
| 619 | View Code Duplication | private function validateAndSanitizeSignedService($signedService) | |
| 633 | |||
| 634 | /** | ||
| 635 | * Validates and sanitizes the signed resource type parameter | ||
| 636 | * | ||
| 637 | * @param string $signedResourceType Specifies the signed resource types | ||
| 638 | * that are accessible with the account | ||
| 639 | * SAS. | ||
| 640 | * | ||
| 641 | * @return string | ||
| 642 | */ | ||
| 643 | View Code Duplication | private function validateAndSanitizeSignedResourceType($signedResourceType) | |
| 657 | |||
| 658 | /** | ||
| 659 | * Validates and sanitizes the signed permissions parameter | ||
| 660 | * | ||
| 661 | * @param string $signedPermissions Specifies the signed permissions for the | ||
| 662 | * account SAS. | ||
| 663 | * @param string $signedResource Specifies the signed resource for the | ||
| 664 | * | ||
| 665 | * @return string | ||
| 666 | */ | ||
| 667 | private function validateAndSanitizeSignedPermissions( | ||
| 687 | |||
| 688 | /** | ||
| 689 | * Validates and sanitizes the signed protocol parameter | ||
| 690 | * | ||
| 691 | * @param string $signedProtocol Specifies the signed protocol for the | ||
| 692 | * account SAS. | ||
| 693 | |||
| 694 | * @return string | ||
| 695 | */ | ||
| 696 | private function validateAndSanitizeSignedProtocol($signedProtocol) | ||
| 709 | |||
| 710 | /** | ||
| 711 | * Checks if a string contains an other string | ||
| 712 | * | ||
| 713 | * @param string $input The input to test. | ||
| 714 | * @param string $toFind The string to find in the input. | ||
| 715 | |||
| 716 | * @return bool | ||
| 717 | */ | ||
| 718 | private function strcontains($input, $toFind) | ||
| 722 | |||
| 723 | /** | ||
| 724 | * Removes duplicate characters from a string | ||
| 725 | * | ||
| 726 | * @param string $input The input string. | ||
| 727 | |||
| 728 | * @return string | ||
| 729 | */ | ||
| 730 | private function validateAndSanitizeStringWithArray($input, array $array) | ||
| 755 | |||
| 756 | |||
| 757 | /** | ||
| 758 | * Generate the canonical resource using the given account name, service | ||
| 759 | * type and resource. | ||
| 760 | * | ||
| 761 | * @param string $accountName The account name of the service. | ||
| 762 | * @param string $service The service name of the service. | ||
| 763 | * @param string $resource The name of the resource. | ||
| 764 | * | ||
| 765 | * @return string | ||
| 766 | */ | ||
| 767 | private static function generateCanonicalResource( | ||
| 784 | } | ||
| 785 | 
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.