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 |
||
24 | class Context |
||
25 | { |
||
26 | /** |
||
27 | * @var \Aimeos\MShop\Context\Item\Iface |
||
28 | */ |
||
29 | private static $context; |
||
30 | |||
31 | /** |
||
32 | * @var \Illuminate\Contracts\Config\Repository |
||
33 | */ |
||
34 | private $config; |
||
35 | |||
36 | /** |
||
37 | * @var \Aimeos\MShop\Locale\Item\Iface |
||
38 | */ |
||
39 | private $locale; |
||
40 | |||
41 | /** |
||
42 | * @var \Illuminate\Session\Store |
||
43 | */ |
||
44 | private $session; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Initializes the object |
||
49 | * |
||
50 | * @param \Illuminate\Contracts\Config\Repository $config Configuration object |
||
51 | * @param \Illuminate\Session\Store $session Laravel session object |
||
52 | */ |
||
53 | public function __construct( \Illuminate\Contracts\Config\Repository $config, \Illuminate\Session\Store $session ) |
||
54 | { |
||
55 | $this->config = $config; |
||
56 | $this->session = $session; |
||
57 | } |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Returns the current context |
||
62 | * |
||
63 | * @param boolean $locale True to add locale object to context, false if not |
||
64 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
65 | */ |
||
66 | public function get( $locale = true ) |
||
67 | { |
||
68 | if( self::$context === null ) |
||
69 | { |
||
70 | $context = new \Aimeos\MShop\Context\Item\Standard(); |
||
71 | |||
72 | $config = $this->getConfig(); |
||
73 | $context->setConfig( $config ); |
||
74 | |||
75 | $dbm = new \Aimeos\MW\DB\Manager\DBAL( $config ); |
||
76 | $context->setDatabaseManager( $dbm ); |
||
77 | |||
78 | $fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) ); |
||
79 | $context->setFilesystemManager( $fs ); |
||
80 | |||
81 | $mq = new \Aimeos\MW\MQueue\Manager\Standard( $config ); |
||
82 | $context->setMessageQueueManager( $mq ); |
||
83 | |||
84 | $mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } ); |
||
85 | $context->setMail( $mail ); |
||
86 | |||
87 | $logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context ); |
||
88 | $context->setLogger( $logger ); |
||
89 | |||
90 | $cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context ); |
||
91 | $context->setCache( $cache ); |
||
92 | |||
93 | self::$context = $context; |
||
94 | } |
||
95 | |||
96 | $context = self::$context; |
||
97 | |||
98 | if( $locale === true ) |
||
99 | { |
||
100 | $localeItem = $this->getLocale( $context ); |
||
101 | $langid = $localeItem->getLanguageId(); |
||
102 | |||
103 | $context->setLocale( $localeItem ); |
||
104 | $context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $langid ) ) ); |
||
105 | } |
||
106 | |||
107 | $session = new \Aimeos\MW\Session\Laravel5( $this->session ); |
||
108 | $context->setSession( $session ); |
||
109 | |||
110 | $this->addUser( $context ); |
||
111 | |||
112 | return $context; |
||
113 | } |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Adds the user ID and name if available |
||
118 | * |
||
119 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
120 | */ |
||
121 | protected function addUser( \Aimeos\MShop\Context\Item\Iface $context ) |
||
122 | { |
||
123 | if( ( $userid = Auth::id() ) !== null ) |
||
124 | { |
||
125 | $context->setUserId( $userid ); |
||
126 | $context->setGroupIds( function() use ( $context, $userid ) |
||
127 | { |
||
128 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' ); |
||
129 | return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups(); |
||
130 | } ); |
||
131 | } |
||
132 | |||
133 | if( ( $user = Auth::user() ) !== null ) { |
||
134 | $context->setEditor( $user->name ); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Creates a new configuration object. |
||
141 | * |
||
142 | * @return \Aimeos\MW\Config\Iface Configuration object |
||
143 | */ |
||
144 | protected function getConfig() |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Returns the locale item for the current request |
||
159 | * |
||
160 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
161 | * @return \Aimeos\MShop\Locale\Item\Iface Locale item object |
||
162 | */ |
||
163 | protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context ) |
||
179 | } |
||
180 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.